Import java. awt .*;
Import javax. swing .*;
Import javax. swing. tree .*;
Import javax. swing. event .*;
// Forest-like relationship diagram
Public class JTreeDemo extends JFrame {
JTextField jtfInfo; // text field used to display the name of the clicked Node
Public JTreeDemo (){
Super ("forest-like Relational Graph"); // calls the parent class Constructor
DefaultMutableTreeNode root = new DefaultMutableTreeNode ("set"); // generate the root node
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode ("regular"); // generate node 1
Node1.add (new DefaultMutableTreeNode ("default path"); // Add a new node to node 1.
Node1.add (new DefaultMutableTreeNode ("Save option "));
Root. add (node1); // add Node 1 to the root node.
Root. add (new DefaultMutableTreeNode ("interface "));
Root. add (new DefaultMutableTreeNode ("voice prompt "));
Root. add (new DefaultMutableTreeNode ("print "));
JTree tree = new JTree (root); // get the JTree instance
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree. getCellRenderer (); // get the Renderer of the JTree
Renderer. setLeafIcon (null); // set the leaf node icon to null.
Renderer. setClosedIcon (null); // set the icon for closing a node to null.
Renderer. setOpenIcon (null); // you can specify the node opening icon as null.
Tree. addTreeSelectionListener (new TreeSelectionListener () {// process the selected node event
Public void valueChanged (TreeSelectionEvent evt ){
TreePath path = evt. getPath (); // obtain the selected path.
String info = path. getLastPathComponent (). toString (); // obtain the selected node name.
JtfInfo. setText (info); // display the name in the text field
}
});
JScrollPane jsp = new JScrollPane (tree); // Add a JTree to the scroll pane.
JtfInfo = new JTextField (); // instantiate a text field
JtfInfo. setEditable (false); // The text field cannot be edited.
GetContentPane (). add (jsp, BorderLayout. CENTER); // add a component to the container.
GetContentPane (). add (jtfInfo, BorderLayout. SOUTH );
SetSize (250,200); // set the window size
SetVisible (true); // sets the window visibility.
Setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // exit the program when the window is closed.
}
Public static void main (String [] args ){
New JTreeDemo ();
}
}