Tree-like diagram of java Implementation

Source: Internet
Author: User

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 ();
}
}

Related Article

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.