Function: Create a tree menu Description: Creating a tree menu structure is similar to creating a menu bar, and is created based on levels and models.
Create root node, child node and grandchild node objects through the DefaultMutableTreeNode class, and then use the DefaultTreeModel
The class uses the root node to create a tree model object, and then inserts the node object into the tree model through the treeModel.insertNodeInto method.
Rendering:
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 Tree() { 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 a TreeModel DefaultTreeModel treeModel = new DefaultTreeModel(root); //Insert child nodes node1,node2 treeModel.insertNodeInto(node1,root,root.getChildCount()); treeModel.insertNodeInto(node2,root,root.getChildCount()); //Create a child node of node node1 and insert 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("Close"); treeModel.insertNodeInto(leafnode,node1,node1.getChildCount()); //Create a child node of node node2 and insert it 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 a tree object JTree tree = new JTree(treeModel); //Set the selection of Tree to only select one node at a time tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION); //Register the listener tree.addTreeSelectionListener(this); tree.setRowHeight(20); //Create node drawing object DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)tree.getCellRenderer(); //Set the font cellRenderer.setFont(new Font("Serif",Font.PLAIN,14)); cellRenderer. setBackgroundNonSelectionColor(Color.white); cellRenderer.setBackgroundSelectionColor(Color.yellow); cellRenderer.setBorderSelectionColor(Color.red); //Set the changing color of the text when selected or not selected cellRenderer.setTextNonSelectionColor(Color.black); cellRenderer.setTextSelectionColor(Color.blue); / /Add the tree object to the content panel container.add(new JScrollPane(tree)); //Create label label = new JLabel("The node you are currently selecting 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 closing action} //Handle the TreeSelectionEvent event public void valueChanged(TreeSelectionEvent event) { JTree tree = (JTree)event.getSource(); //Get the currently selected node DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); String nodeName = selectionNode.toString (); label.setText("The node you are currently selecting is: "+nodeName); } public static void main(String args[]) { Tree d = new Tree(); } }
The tree menu created in this article according to levels and models is similar to creating a menu bar. I wonder if your friends have mastered it?