Download presentation
Presentation is loading. Please wait.
Published byClaude Leo Sanders Modified over 8 years ago
1
Advanced Swing Trees
2
Contents I.Introduction to Trees II. Simple Trees III. Editing Trees and Tree Paths IV. Node Enumeration V. Rendering Nodes VI. Listening to Tree Events
3
I. Introduction To Trees
4
Tree Terminology
5
Tree Traversal Orders
6
Java Tree Classes
7
II. Simple Trees ● To construct a JTree, supply the tree model in the constructor: TreeModel model =...; JTree tree = new JTree(model); ● How do you obtain a tree model? ● Creating a class that implements the TreeModel interface. ● Using the DefaultTreeModel that the Swing library supplies. To construct a default tree model, you must supply a root node. TreeNode root =...; DefaultTreeModel model = new DefaultTreeModel(root);
8
How do you obtain a tree node? ● Implement the TreeNode interface. ● Using the DefaultMutableTreeNode. ● This class implements the MutableTreeNode interface, a subinterface of TreeNode ● A default mutable tree node holds an object, the user object. The tree renders the user objects for all nodes. Unless you specify a renderer, the tree simply displays the string that is the result of the toString method.
9
Setting User Objects ● You can specify the user object in the constructor, DefaultMutableTreeNode node = new DefaultMutableTreeNode("Texas"); ● or you can set it later with the setUserObject method.... node.setUserObject("California");
10
Establishing the Parent/Child Relationships ● Start with the root node, and use the add method to add the children: ● DefaultMutableTreeNode root = new DefaultMutableTreeNode("World"); DefaultMutableTreeNode country = new DefaultMutableTreeNode("USA"); root.add(country); DefaultMutableTreeNode state = new DefaultMutableTreeNode("California"); country.add(state);
11
Constructing a JTree With a Root Node ● Construct a JTree with the tree model DefaultTreeModel treeModel = new DefaultTreeModel(root); JTree tree = new JTree(treeModel); ● Construct a JTree with a root node JTree tree = new JTree(root); ● Then the tree automatically constructs a default tree model.
14
III. Editing Trees and Tree Paths ● The JTree class has a way of identifying nodes in a tree. ● It does not deal with tree nodes, but with paths of objects, called tree paths. A tree path starts at the root and consists of a sequence of child nodes ● The TreePath class manages a sequence of Object (not TreeNode !) references.
15
Getting the Selected Node TreePath selectionPath = tree.getSelectionPath(); DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) selectionPath.getLastPathComponent(); ● There is a convenience method that gives the selected node immediately: DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
16
Adding A New Node ● Insert a new node ● void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) ● Remove a node ● void removeNodeFromParent(MutableTreeNode node) ● Notify the tree model listeners that node has changed ● void nodeChanged(TreeNode node)
17
Making a New Node Visible ● To expand all parent nodes so that the newly added node becomes visible TreeNode[] nodes = model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); tree.makeVisible(path); ● To expand all nodes along the path, and scroll the node at the end of the path into view tree.scrollPathToVisible(path);
18
● By default, tree nodes cannot be edited. To allow the user to edit a node ● tree.setEditable(true);
19
Editing a Tree
24
IV. Node Enumeration ● To find a node in a tree by starting at the root and visiting all children until you have found a match
25
● Here is the typical usage pattern: Enumeration breadthFirst = node.breadthFirstEnumeration(); while (breadthFirst.hasMoreElements()) do something with breadthFirst.nextElement(); ● Enumeration breadthFirstEnumeration() ● Enumeration depthFirstEnumeration()
26
V. Rendering Nodes ● To change the way in which a tree component draws the nodes. ● All these changes are made possible by installing a new tree cell renderer into the tree. ● By default, the JTree class uses DefaultTreeCellRenderer objects to draw each node. ● The DefaultTreeCellRenderer class extends the JLabel class. The label contains the node icon and the node label.
27
● You can customize the display in three ways. 1.You can change the icons, font, and background color used by a DefaultTreeCellRenderer. These settings are used for all nodes in the tree. 2.You can install a renderer that extends the DefaultTreeCellRenderer class and vary the icons, fonts, and background color for each node. 3.You can install a renderer that implements the TreeCellRenderer interface, to draw a custom image for each node.
28
1. Using a DefaultTreeCellRenderer object DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); renderer.setLeafIcon(new ImageIcon( "blue-ball.gif")); // used for leaf nodes renderer.setClosedIcon(new ImageIcon( "red-ball.gif")); // used for collapsed nodes renderer.setOpenIcon(new ImageIcon( "yellow-ball.gif")); // used for expanded nodes tree.setCellRenderer(renderer);
29
2. Extending the DefaultTreeCellRenderer class class MyTreeCellRenderer extends DefaultTreeCellRenderer{ public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus){ super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; look at node.getUserObject(); Font font = appropriate font; setFont(font); return this; } };
30
V. Listening to Tree Events public interface TreeSelectionListener { void valueChanged(TreeSelectionEvent event) } ● That method is called whenever the user selects or deselects tree nodes. ● Adding a tree selection listener to a tree tree.addTreeSelectionListener(listener);
31
Tree Selection Modes ● The JTree class uses a TreeSelectionModel to manage node selection. ● TreeSelectionModel.SINGLE_TREE_SELECTION ● TreeSelectionModel.CONTIGUOUS_TREE_SELECTION ● TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION ● For example: tree.getSelectionModel().setSelectionMode( TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
32
Class Browser
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.