Import Java. AWT. dimension; import Java. AWT. color; import javax. swing. jframe; import javax. swing. jpanel; import javax. swing. jscrollpane; import javax. swing. jtree; import javax. swing. boxlayout; import javax. swing. tree. treepath; import javax. swing. tree. defaultmutabletreenode; import javax. swing. tree. defaulttreemodel;/* jtree constructor: jtree () jtree (hashtable value) jtree (object [] value) // only this constructor can create multiple root nodes jtree (treemodel newmodel) jtree (treenode root, Boolean asksallowschildren) jtree (vector value) */public class jtreedemo {public static void main (string [] ARGs) {// constructor: jtree () jtree example1 = new jtree (); // constructor: jtree (object [] value) object [] letters = {"A", "B", "C", "D", "E "}; jtree example2 = new jtree (letters); // constructor: jtree (treenode root) (treenode empty) // create defaultmutabletreenode node1 = new defaultmutabletreenode () with empty knots (); // define the tree node jtree example3 = new jtree (node1); // use this tree node as a parameter to call the jtree constructor to create a tree containing a root node // constructor: jtree (treenode root) (same as above, but not empty in treenode) // use a root node to create defaultmutabletreenode node2 = new defaultmutabletreenode ("color"); jtree example4 = new jtree (node2 ); // The node cannot be colored. The default value is example4.setbackground (color. lightgray); // constructor: jtree (treenode root, Boolean asksallowschildren) (same as above, but different from treenode) // use the defaultmutabletreenode class to create a tree with a root node, set to add child nodes, and then add defaultmutabletreenode color = new defaultmutabletreenode ("color", true); defaultmutabletreenode gray = new defaultmutabletreenode ("gray"); color. add (Gray); color. add (New defaultmutabletreenode ("red"); gray. add (New defaultmutabletreenode ("lightgray"); gray. add (New defaultmutabletreenode ("darkgray"); color. add (New defaultmutabletreenode ("green"); jtree example5 = new jtree (color); // constructor: jtree (treenode root) (same as above, but not empty) // Add the node one by one to create defaultmutabletreenode biology = new defaultmutabletreenode ("biology"); defaultmutabletreenode animal = new defaultmutabletreenode ("animal "); defaultmutabletreenode mammal = new defaultmutabletreenode ("Mammal"); defaultmutabletreenode horse = new defaultmutabletreenode ("horse"); mammal. add (Horse); animal. add (mammal); biology. add (animal); jtree example6 = new jtree (biology); horse. isleaf (); horse. isroot (); // constructor: jtree (treemodel newmodel) // use the defaultmutabletreenodel class to define a node and then use this node as a parameter to define a model using defatretreemode // create a tree, use the jtree constructor to create a tree defaultmutabletreenode root = new defaultmutabletreenode ("root1"); defaultmutabletreenode Child1 = new defaultmutabletreenode ("Child1 "); defaultmutabletreenode child11 = new defaultmutabletreenode ("child11"); defaultmutabletreenode child111 = new defaultmutabletreenode ("child111"); root. add (Child1); child1.add (child11); child11.add (child111); defaulttreemodel model = new defaulttreemodel (Root); jtree example7 = new jtree (model ); jpanel Panel = new jpanel (); panel. setlayout (New boxlayout (panel, boxlayout. x_axis); panel. setpreferredsize (new dimension (700,400); panel. add (New jscrollpane (example1); // The jtree must be placed on the jscrollpane panel. add (New jscrollpane (example2); panel. add (New jscrollpane (example3); panel. add (New jscrollpane (example4); panel. add (New jscrollpane (example5); panel. add (New jscrollpane (example6); panel. add (New jscrollpane (example7); jframe frame = new jframe ("jtreedemo"); frame. setdefaclocloseoperation (jframe. exit_on_close); frame. setcontentpane (panel); frame. pack (); frame. show ();}}
In the actual development process, the jtree component is often used, and you may encounter one or more problems. Here, we will write down some occasional experiences and share them with you, hoping to help you.
Private jtree jtnetdevice; // number component Declaration
Private jscrollpane jsptree; // scroll panel Declaration
1. Initialization
Defaultmutabletreenode rootnode = new defaultmutabletreenode ("root ");
Jtnetdevice = new jtree (rootnode );
Jtnetdevice. setautoscrolls (true );
Gettreeselectionmodel (). setselectionmode (treeselectionmodel. single_tree_selection); // you can specify the single-choice mode.
Jsptree = new jscrollpane ();
Jsptree. getviewport (). Add (jtnetdevice, null );
2. Three frequently used value functions
Private defatretreemodel gettreemodel (){
Return (defatretreemodel) jtnetdevice. GetModel ();
}
Private defaultmutabletreenode getrootnode (){
Return (defaultmutabletreenode) gettreemodel (). getroot ();
}
Private treeselectionmodel gettreeselectionmodel (){
Return jtnetdevice. getselectionmodel ();
}
3. Obtain the path based on the node:
Treepath visiblepath = new treepath (gettreemodel (). getpathtoroot (node ));
4. expand to the node according to the path
Jtnetdevice. makevisible (visiblepath );
5. Set the Node Selection Based on path
Jtnetdevice. setselectionpath (visiblepath );
6. method of selecting a node
First, obtain the tree path based on the node. chosen is the node to be selected.
Treepath visiblepath = new treepath (defaulttreemodel) jtnetdevice. GetModel ()).
Getpathtoroot (chosen ));
Select the node based on the path.
Jtnetdevice. setselectionpath (visiblepath );
7. scroll to the visible position
Jtnetdevice. scrollpathtovisible (visiblepath );
8. Right-click the jtree and choose the pop-up menu.
Void jtnetdevice_mousereleased (mouseevent e ){
If (E. ispopuptrigger ()){
Jpopupmenu1.show (E. getcomponent (), E. getx (), E. Gety (); // right-click the menu
}
}
9. Expand jtree
// If expand is true, expands all nodes in the tree.
// Otherwise, collapses all nodes in the tree.
Public void expandall (jtree tree, Boolean expand ){
Treenode root = (treenode) tree. GetModel (). getroot ();
// Traverse tree from Root
Expandall (tree, new treepath (Root), expand );
}
Private void expandall (jtree tree, treepath parent, Boolean expand ){
// Traverse children
Treenode node = (treenode) parent. getlastpathcomponent ();
If (node. getchildcount ()> = 0 ){
For (enumeration E = node. Children (); E. hasmoreelements ();){
Treenode n = (treenode) E. nextelement ();
Treepath Path = parent. pathbyaddingchild (N );
Expandall (tree, path, expand );
}
}
// Expansion or collapse must be done bottom-up
If (expand ){
Tree. expandpath (parent );
} Else {
Tree. collapsepath (parent );
}
}
10. How to traverse jtree
// Create a tree
Jtree tree = new jtree ();
// Add a tree node ......
// Traverse all nodes
Visitallnodes (tree );
// Only traverse the expanded nodes
Visitallexpandednodes (tree );
// Traverse all nodes in tree
Public void visitallnodes (jtree tree ){
Treenode root = (treenode) tree. GetModel (). getroot ();
Visitallnodes (Root );
}
Public void visitallnodes (treenode node ){
// Node is visited exactly once
Process (node );
If (node. getchildcount ()> = 0 ){
For (enumeration E = node. Children (); E. hasmoreelements ();){
Treenode n = (treenode) E. nextelement ();
Visitallnodes (N );
}
}
}
// Traverse all expanded nodes in tree
Public void visitallexpandednodes (jtree tree ){
Treenode root = (treenode) tree. GetModel (). getroot ();
Visitallexpandednodes (tree, new treepath (Root ));
}
Public void visitallexpandednodes (jtree tree, treepath parent ){
// Return if node is not expanded
If (! Tree. isvisible (parent )){
Return;
}
// Node is visible and is visited exactly once
Treenode node = (treenode) parent. getlastpathcomponent ();
Process (node );
// Visit all children
If (node. getchildcount ()> = 0 ){
For (enumeration E = node. Children (); E. hasmoreelements ();){
Treenode n = (treenode) E. nextelement ();
Treepath Path = parent. pathbyaddingchild (N );
Visitallexpandednodes (tree, PATH );
}
}
}