Java Implementation tree structure

Source: Internet
Author: User

 packagetree;Importjava.awt.BorderLayout;Importjava.awt.Color;Importjava.awt.Font;Importjava.awt.Image;Importjavax.swing.ImageIcon;Importjavax.swing.JFrame;Importjavax.swing.JLabel;Importjavax.swing.JScrollPane;Importjavax.swing.JTree;Importjavax.swing.event.TreeSelectionEvent;Importjavax.swing.event.TreeSelectionListener;Importjavax.swing.tree.DefaultMutableTreeNode;Importjavax.swing.tree.DefaultTreeCellRenderer;Importjavax.swing.tree.DefaultTreeModel;Importjavax.swing.tree.TreeSelectionModel; public classTreeNodeextendsJFrameImplementsTreeselectionlistener{JLabel jl;  publicTreeNode () {Super("tree-shaped structure"); //Create a root node and a child nodeDefaultmutabletreenode root=NewDefaultmutabletreenode ("text editor"); Defaultmutabletreenode Node1=NewDefaultmutabletreenode ("file"); Defaultmutabletreenode Node2=NewDefaultmutabletreenode ("edit"); //creating TreeModel with the root nodeDefaulttreemodel treemodel=NewDefaulttreemodel (root); //Inserting child nodestreemodel.insertnodeinto (node1, root, root.getchildcount ());                Treemodel.insertnodeinto (node2, root, Root.getchildcount ()); //Create a Node1 child node and insertDefaultmutabletreenode leafnode=NewDefaultmutabletreenode ("open");                Treemodel.insertnodeinto (leafnode, node1, Node1.getchildcount ()); Leafnode=NewDefaultmutabletreenode ("save");                Treemodel.insertnodeinto (leafnode, node1, Node1.getchildcount ()); Leafnode=NewDefaultmutabletreenode ("save as");                Treemodel.insertnodeinto (leafnode, node1, Node1.getchildcount ()); Leafnode=NewDefaultmutabletreenode ("off");                Treemodel.insertnodeinto (leafnode, node1, Node1.getchildcount ()); //Create a Node2 child node and insertLeafnode=NewDefaultmutabletreenode ("cut");                Treemodel.insertnodeinto (leafnode, node2, Node2.getchildcount ()); Leafnode=NewDefaultmutabletreenode ("copy");                Treemodel.insertnodeinto (leafnode, node2, Node2.getchildcount ()); Leafnode=NewDefaultmutabletreenode ("paste");                Treemodel.insertnodeinto (leafnode, node2, Node2.getchildcount ()); //to Create a Tree objectJTree tree =NewJTree (treemodel);        Tree.getselectionmodel (). Setselectionmode (treeselectionmodel.single_tree_selection); Tree.addtreeselectionlistener ( this); Tree.setrowheight (20); //Creating a node drawing objectDefaulttreecellrenderer cell=(defaulttreecellrenderer) Tree.getcellrenderer (); //Set FontCell.setfont (NewFont ("Serif", font.plain,14));        Cell.setbackgroundnonselectioncolor (color.white);        Cell.setbackgroundselectioncolor (color.light_gray);                Cell.setforeground (color.red); Cell.seticon (NewImageIcon ("/image/folder.jpg")); //set the color of text when checked and uncheckedCell.settextnonselectioncolor (color.black);                        Cell.settextnonselectioncolor (color.black);  this. Add (NewJScrollPane (tree)); JL=NewJLabel ("the node You are currently selecting is:", jlabel.center); Jl.setfont (NewFont ("Serif", font.plain,14));  this. Add (jl,borderlayout.south);  this. setSize (400, 400);  this. setvisible (true);  this. Setdefaultcloseoperation (jframe.exit_on_close); }             public Static voidmain (string[] Args) {//TODO auto-generated Method StubTreeNode t=NewTreeNode (); } @Override public voidvaluechanged (treeselectionevent E) {//TODO auto-generated Method StubJTree Tree=(jtree) E.getsource (); //gets the currently selected nodeDefaultmutabletreenode selectnode=(defaultmutabletreenode) tree.getlastselectedpathcomponent (); String nodename=selectnode.tostring (); Jl.settext ("the node you are currently selecting is:" +nodename); }}

Java Implementation tree structure

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.