Function: Implement Create a tree menu
Description: Creating a tree menu structure is similar to creating a menu bar, and is created by hierarchy and model.
Create the root node, child nodes, and grandchild node objects through the Defaultmutabletreenode class, and then pass the Defaulttreemodel
The class uses the root node to create the tree model object and then inserts the node object into the tree model through the Treemodel.insertnodeinto method.
Effect Chart:
Code:
Import java.awt.*;
Import javax.swing.*;
Import javax.swing.tree.*;
Import javax.swing.event.*;
public class Tree extends JFrame implements Treeselectionlistener {private JLabel label;
Public trees () {Super ("tree Menu"); SetSize (400,400);
Container Container = Getcontentpane ();
Create root node and child node Defaultmutabletreenode root = new Defaultmutabletreenode ("text editor");
Defaultmutabletreenode Node1 = new Defaultmutabletreenode ("file");
Defaultmutabletreenode Node2 = new Defaultmutabletreenode ("edit");
Use the root node to create TreeModel Defaulttreemodel TreeModel = new Defaulttreemodel (root);
Insert child node Node1,node2 Treemodel.insertnodeinto (Node1,root,root.getchildcount ());
Treemodel.insertnodeinto (Node2,root,root.getchildcount ());
Creates a node Node1 and inserts Defaultmutabletreenode Leafnode = new Defaultmutabletreenode ("open");
Treemodel.insertnodeinto (Leafnode,node1,node1.getchildcount ());
Leafnode = new Defaultmutabletreenode ("Save"); Treemodel.insertnodeinto (leafnode,node1,nOde1.getchildcount ());
Leafnode = new Defaultmutabletreenode ("Save As");
Treemodel.insertnodeinto (Leafnode,node1,node1.getchildcount ());
Leafnode = new Defaultmutabletreenode ("Off");
Treemodel.insertnodeinto (Leafnode,node1,node1.getchildcount ());
Creates a node Node2 and inserts Leafnode = new Defaultmutabletreenode ("cut");
Treemodel.insertnodeinto (Leafnode,node2,node2.getchildcount ());
Leafnode = new Defaultmutabletreenode ("copy");
Treemodel.insertnodeinto (Leafnode,node2,node2.getchildcount ());
Leafnode = new Defaultmutabletreenode ("paste");
Treemodel.insertnodeinto (Leafnode,node2,node2.getchildcount ());
Create tree object JTree trees = new JTree (TreeModel);
Set the tree selection to only one node at a time Tree.getselectionmodel (). Setselectionmode (treeselectionmodel.single_tree_selection);
Register Listener Tree.addtreeselectionlistener (this);
Tree.setrowheight (20);
Create a node drawing object defaulttreecellrenderer Cellrenderer = (defaulttreecellrenderer) tree.getcellrenderer (); Set upFont Cellrenderer.setfont (New font ("Serif", font.plain,14));
Cellrenderer.setbackgroundnonselectioncolor (Color.White);
Cellrenderer.setbackgroundselectioncolor (Color.yellow);
Cellrenderer.setborderselectioncolor (color.red);
Set the color Cellrenderer.settextnonselectioncolor (color.black) for the change of text when selected or not selected;
Cellrenderer.settextselectioncolor (Color.Blue);
Add the tree object to the Content panel Container.add (new JScrollPane);
Create a label label = new JLabel ("Your currently selected node is:", jlabel.center);
Label.setfont (New Font ("Serif", font.plain,14));
Container.add (Label,borderlayout.south); SetVisible (TRUE); Set Visible setdefaultcloseoperation (Jframe.exit_on_close); Set window Close Action}//Handle treeselectionevent event public void ValueChanged (Treeselectionevent event) {JTree tree = (jtre
e) Event.getsource ();
Gets the currently selected node Defaultmutabletreenode Selectionnode = (defaultmutabletreenode) tree.getlastselectedpathcomponent ();
String nodename = selectionnode.tostring (); Label.settext ("Your currently selected sectionPoint is: "+nodename);
public static void Main (String args[]) {Tree d = new tree (); }
}
This article is a hierarchical and model-created tree menu, similar to creating a menu bar, do not know whether the small partners have mastered it?