[JAVA100 example]076, using swing in applets

Source: Internet
Author: User
Tags readline first row trim

Import java.awt.*;


import java.awt.event.*;


import javax.swing.*;


import javax.swing.event.*;


import javax.swing.tree.*;


import java.io.*;


import java.net.*;


import java.util.*;


import java.applet.*;


/**


* <p>title:applet using swing</p>


* <p>description: Use Swing's japplet to implement tree functions. </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:PageTree.java</p>


* @version 1.0


*/


public class Pagetree extends JApplet implements Treeselectionlistener {


defaultmutabletreenode[] nodeat = new defaultmutabletreenode[256];


String target = null;


Hashtable links = new Hashtable ();


Appletcontext context;


public void init () {


//Get the contents of the applet


context = getAppletContext ();


//define root node


Defaultmutabletreenode root = null;


Getcontentpane (). setlayout (New BorderLayout ());


//Get parameters. The data configuration file that is defined.


String s = getparameter ("file");


if (s!= null) {


try {


//Use URL to open the data file for use on the network.


url url = new URL (getdocumentbase (), s);


bufferedreader br = new BufferedReader (


New InputStreamReader (Url.openstream (), "Jisautodetect"));


//Read the first row of data, if empty then do not perform


String line = Br.readline ();


if (line = = null) return;


root = new Defaultmutabletreenode (line);


nodeat[0] = root;


int level = 0;


while (line = Br.readline ())!= null) {


//Use "|" Separate data


StringTokenizer st = new StringTokenizer (line, "|");


String token = St.nexttoken ();


int n = countspaces (token);


//Add node name


defaultmutabletreenode node = new Defaultmutabletreenode (Token.trim ());


//COMPUTE Node Series


for (int i = level + 1; i < n; i++) {


Defaultmutabletreenode dummy = new Defaultmutabletreenode ("");


Nodeat[i].add (dummy);


nodeat[i+1] = dummy;


   }


level = n;


nodeat[n].add (node);


nodeat[n+1] = node;


//To "|" The data after the symbol is processed


if (St.hasmoretokens ()) {


token = St.nexttoken ();


try {


url = new URL (Token.trim ());


links.put (node, URL);


} catch (Malformedurlexception ex) {


Ex.printstacktrace ();


   }


   }


  }


} catch (Malformedurlexception e) {


E.printstacktrace ();


} catch (IOException e) {


E.printstacktrace ();


  }


s = getparameter ("target");


if (s!= null) target = S.trim ();


JTree tree = new JTree (root);


///Add Tree Selection Monitor


Tree.addtreeselectionlistener (this);


JScrollPane jsp = new JScrollPane (tree);


Getcontentpane (). Add (JSP, Borderlayout.center);


 }


}


/**


*<br> Method Description: Calculate the number of spaces


*<br> input Parameters:


*<br> return type:


*/


private int countspaces (String s) {


int n = 0;


while (S.charat (n) ==´´) {


n++;


 }


return n;


}


/**


*<br> Method Description: Tree selection monitoring Implementation method


*<br> input parameter: treeselectionevent e Select tree Node event


*<br> return type:


*/


public void valuechanged (Treeselectionevent e) {


TreePath Path = E.getpath ();


Object o = path.getlastpathcomponent ();


url = (URL) links.get (o);


if (URL!= null) {


System.out.println (Links.get (o));


//Open the connection to notify the browser to load the Web page (using the browser to open the applet)


context.showdocument (URL);


 }


}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.