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)
Jtree (treenode root, Boolean asksallowschil.pdf)
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 null)
// Use empty knots to create achievements
Defaultmutabletreenode node1 = new defaultmutabletreenode (); // defines the tree node
Jtree example3 = new jtree (node1); // call the jtree constructor to create a tree containing a root node as a parameter using this tree node
// Constructor: jtree (treenode root) (same as above, but treenode is not empty)
// Create a tree with a root node
Defaultmutabletreenode node2 = new defaultmutabletreenode ("color ");
Jtree example4 = new jtree (node2); // The node cannot be colored. The default value is white-faced black.
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 it to add a child node, and then add a child node
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 treenode is not empty)
// Add knots one by one to create achievements
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 use this node as a parameter to define a defatretreemode.
// Create a tree model and 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 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 ();
}
}