Implement an infinite tree structure with Java applet applet

xiaoxiao2021-03-06  113

This program can create a tree according to the information dynamics read from the specified XML file.

1. The tree is characterized:

1) Unlimited level tree structure

Theoretically, the depth of the tree can be infinite, the depth of the tree is determined by the information given by the XML file;

Data loading uses a combination of loading and dynamic loading;

Re-load the tree structure each time the page is turned on, and the dynamics of the establishment of the tree is truly achieved.

2) Reuse

Since this tree is established by reading the XML file, the tree code is fixed, so it has strong reuse. Just give an XML file, the program can establish a corresponding tree.

3) Interaction with users

Since the program is implemented with a Java applet, you can listen to the class in the Java language to respond to user operation. Have better interactability with the user.

2. Program structure

This tree mainly has three types of implementation:

1) Class TREENODE: This class serves to describe the data structure of the tree, and provides some setter, the getter function is used to set the properties of the tree structure.

2) Class IconRender: This class is also an auxiliary class that sets icons for the tree node. And the icon can be changed according to the selected state of the tree node.

3) Class AppletTree: This class is the primary class that establishes this tree, which inherits from the Applet and implements the TreeseLectionListener interface to listen to user operations. The tree structure is displayed by Java class JTree, and we only need reasonable setting of the attribute of JTREE, you can construct this tree. And its attribute is provided through the ELEMENT in the JDOM. The listening user operation has been mentioned above is done by implementing the TreeseLectionListener interface, but only listening is nothing to respond. How can I use the listened user to pass to the corresponding class to respond to the user's operation? The method used here is to pass the parameters to the JSP file through the URL, and then pass it to the corresponding operation class by the JSP file.

The code of these three classes is as follows:

1. TREENODE class

Public class treenode {

Private string id;

PRIVATE STRING NAME;

PRIVATE STRING LINK;

Public Treenode (String ID, String Name, String Link) {

THIS.ID = ID;

THIS.NAME = Name;

This.Link = LINK;

}

Public string getId () {

Return ID;

}

Public void setid (String ID) {

THIS.ID = ID;

}

Public void setname (String name) {

THIS.NAME = Name;

}

Public string getname () {

Return Name;

}

Public string toString () {

Return Name;

}

Public string getLink () {

Return Link;

}

Public void setLink (String link) {

This.Link = LINK;

}

}

2. IconRender class

Import javax.swing. *;

Import java.awt. *;

Import javax.swing.tree. *;

Import javax.swing.tree.defaulttreecellrenderer;

Iconrender

Class extends defaulttreecellrenderer {

// Create icon for display

Public static final imageicon leafselectedicon = new imageicon ("OpenedFolder.ico"); public static final imageicon leafunselectedicon = new imageicon ("ClosedFolder.ico");

Public static final imageicon folderopen = new imageicon ("OPENEDFolder.ico");

Public static final imageicon folderclose = new imageicon ("closedfolder.ico");

// Set the change in the icon with the selected state

Public Component GettreecellRendereComponent (JTree

Tree, Object Value, Boolean SELECTED, Boolean Expanded,

Boolean Leaf, Int Row, Boolean Hasfocus

{

Super.gettreecellrendererComponent (Tree, Value, SELECTED, Expanded, Leaf, Row, Hasfocus)

IF (Leaf && SELECTED {

Seticon (iconrender.leafselected);

}

ELSE IF (leaf) {

Seticon (iconRender.leafunselected);

}

Return this;

}

Public iconrender () {

Super ();

IF (Leafselectedicon! = NULL) && (LeafunselectedIn! = NULL))

System.out.println ("iconrender: trace");

Setleaficon (Leafunselected);

Setopenicon (Folderopen);

SetCloseDicon (FolderClose);

}

}

3. AppleTtree class

Import javax.swing.event. *;

Import java.awt. *;

Import java.applet. *;

Import javax.swing. *;

Import javax.swing.tree. *;

Import java.awt.event. *;

Import Org.jdom. *;

// Import org.w3c.dom.nodelist;

Import org.jdom.Input. *;

Import java.io. *;

Import java.util. *;

Import java.net. *;

Import java.awt. *;

Import javax.swing. *;

Import javax.swing.border. *;

Import javax.swing.plaf. *;

Import javax.swing.plaf.basic. *;

Import javax.swing.plaf.metal. *;

Import java.io. *;

// Import netscape.javascript. *;

Class MyWindowListener Extends WINDOWADAPTER

{

Public void WINDOWCLOSIG (WindowEvent E)

{

System.exit (1);

}

}

Public Class AppletTree Extends Applet Implements TreeselectionListener {

Private Jtree Tree;

PRIVATE TREEPATH PATH;

Private JscrollPane TreeView;

Private defaultmutabletreenode top;

Private defaultmutabletreenode clicknode;

PRIVATE STRING LINK;

Public applettree () {

}

MouseListener ml = new mouseadapter () {

Public void mousepressed (mouseevent e) {

Int SELROW = Tree.GetrowForLocation (E.GETX (), E.GETY ());

TreePath Selpath = Tree.getPathforLocation (E.GETX (), E.GETY ());

IF (SELROW! = -1) {

IF (E.GetClickCount () == 1) {

System.out.print ("click on a mouse");

// Get the last node

DefaultmutableTreenode Treenode = (DefaultmutableTreenode) Selpath.getLastPathComponent ();

Boolean isleaf = Treenode.isleaf ();

Treenode Objtreenode = (Treenode) Treenode.getuserObject ();

System.out.println (isleaf);

System.out.println (Objtreenode .GetName ());

System.out.println (Objtreenode .GetId ());

IF (Isleaf)

{// If you pass the leaves node to perform query operation

Try

{

GetAppletContext (). showdocument ("http://127.0.0.1:8080/tree/showdevice.htm"), "F2");

}

Catch (Exception EX)

{

System.out.print (ex);

}

}

}

ELSE IF (E.GetClickCount () == 2) {

System.out.print ("Click two mouse");

//getappletcontext().showdocument(new URL ("http://www.9cbs.net/));

// mydoubleclick (selrow, selpath);

}

}

}

}

Public void start ()

{

Super.start ();

}

Public void init () {

Try {

Super.init ();

THIS.SetLayout (New GridLayout);

Tree = CreateTree (New FileInputStream ("E: /EXAMPLE/treexml.xml");

Tree.getSelectionModel (). setSelectionMode (TreeselectionModel.single_tree_selection);

Tree.putClientProperty ("jtree.LinesTyle", "Angled");

Tree.setshowsroTHandles (TRUE);

Tree.setedItable (TRUE); Tree.AddtreeselectriTeListener (this);

Iconrender Render = new iconrender ();

Tree.setcellrenderer (render);

TreeView = New Jscrollpane (Tree);

T //TopPanel.Add (Tree);

THIS.ADD (TreeView);

Tree.AddmouseListener (ml);

} catch (exception e) {

E.PrintStackTrace ();

}

}

Public JTREE CREATETREE (InputStream IS) {

Saxbuilder Builder = new saxbuilder ();

Try {

Document doc = builder.build (is);

Element root = doc.getrootelEment ();

Treenode rootnode = new

Treenode (Root.GetaTributevalue ("ID"), root.getattributevalue ("name"), root.getattributevalue ("havechildren");

TOP = New DefaultMutableTreenode (rootnode);

AddNode (root, top);

} Catch (exception ex) {

EX.PrintStackTrace ();

}

/ / Can change the color of the JTree in JTREE here

UIManager.put ("tree.hash", new coloruiresource (color.red);

Return New JTree (TOP);

}

/ **

*

* @Param E to join the JDOM element in the tree

* @Param FatherNode tree node father node

* /

Private Void AddNode (Element E, DEFAULTMUTABLETREENODE FATHERNODE) ​​{

Try {

Iterator it = E.GetChildren (). Iterator ();

IF (! it.hasnext ())

Return;

While (it.hasnext ()) {

ELEMENT SUB = (ELEMENT) IT.NEXT ();

String id = new string (Sub.GetaTributevalue ("ID"). GetBytes (), "GB2312");

String name = new string (Sub.getaTributevalue ("name"). GetBytes (), "GB2312");

String link = new string (Sub.GetaTributevalue ("havechildren"). GetBytes (), "GB2312");

System.out.println (ID);

System.out.println (Name);

System.out.println (LINK);

Treenode TempNode = New Treenode (ID, Name, Link);

DEFAULTMUTABLETREENODE NODE = New DefaultMutableTreenode (TEMPNODE);

FatherNode.Add (Node);

IF (Link.Equals ("True")) {

System.out.println ("True ~! Equal Test");

AddNode (SUB, NODE);

}

}

}

Catch (Exception Exp)

{

System.out.Println ("Encoding Fail");

}

}

/ **

* Find tree nodes according to ID, //

* @Param ID Node ID

* @Param rootnode tree root node

* @return defaultmutabletreenode

* /

Private defaultmutabletreenode gettreenode (String ID, defaultmutabletreenode rootnode) {

Defaultmutabletreenode returncture = null;

IF (rootnode! = null) {

ENUMERATION enum = rootnode.breadthfirstence ();

While (enum.hasmoreElements ()) {

DEFAULTMUTABLETREENODE TEMP = (DefaultmutableTreenode) ENUM.NEXTELEMENT ();

Treenode Node = (Treenode) Temp.getuserObject ();

IF (Node.GetId (). Equals (ID)) {

ReturnNode = TEMP;

Break;

}

}

}

Return ReturnNode;

}

Public void valuechanged (treselectionEvent evenet) {

if (Event.getsource () == Tree) {

PATH = Event.getPath ();

ClickNode = (DefaultmutableTreenode) path.getlastpathcomponent ();

Object Uo = Clicknode.getuserObject ();

Uo InstanceOf Treenode {

Treenode Nd = (Treenode) ClickNode.getuserObject ();

Link = nd.getlink ();

}

}

}

Public static void main (String [] args) {

JFRAME FRAME1 = New JFrame ("Test");

Applettree Tree = New AppletTree ();

Tree.init ();

Frame1.getContentPane (). Add (Tree);

Frame1.setsize (600,600);

Frame1.show ();

Frame1.addwindowlistener (New MyWindowListener ());

}

}

转载请注明原文地址:https://www.9cbs.com/read-101851.html

New Post(0)