Introduction to Java Programming-Comprehensive Version (6th Edition)
31.11. TreePath and TreeSelectionModel
The JTree class contains the methods for selecting tree paths. The TreePath class represents a path from an ancestor to a descendant in a tree. Figure 31.23 shows TreePath .
Figure 31.23. TreePath represents a path from an ancestor to a descendant in a tree.
You can construct a TreePath from a single object or an array of objects, but often instances of TreePath are returned from the methods in JTree and TreeSelectionModel . For instance, the getLeadSelectionPath() method in JTree returns the path from the root to the selected node. There are many ways to extract the nodes from a tree path. Often you use the getLastPathComponent() method to obtain the last node in the path, and then the getParent() method to get all the nodes in the path upward through the link.
The selection of tree nodes is defined in the TreeSelectionModel interface, as shown in Figure 31.24. The DefaultTreeSelectionModel class is a concrete implementation of the TreeSelectionModel that maintains an array of TreePath objects representing the current selection. The last TreePath selected, called the lead path , can be obtained using the getLeadSelectionPath() method. To obtain all the selection paths, use the getSelectionPaths() method, which returns an array of tree paths.
Figure 31.24. The TreeSelectionModel handles selection in a tree.
TreeSelectionModel supports three selection modes: contiguous selection, discontiguous selection, and single selection. Single selection allows only one item to be selected. Contiguous selection allows multiple selections, but the selected items must be contiguous. Discontigous selection is the most flexible; it allows any item to be selected at a given time. The default tree selection mode is discontiguous. To set a selection mode, use the setSelectionMode(int mode) method in TreeSelectionModel . The constants for the three modes are:
-
CONTIGUOUS_TREE_SELECTION
-
DISCONTIGUOUS_TREE_SELECTION
-
SINGLE_TREE_SELECTION
Note
| When you create a JTree , a DefaultTreeSelectionModel is automatically created, and thus you rarely need to create an instance of TreeSelectionModel explicitly. Since most of the methods in TreeSelectionModel are also in JTree , you can get selection paths and process the selection without directly dealing with TreeSelectionModel . |
Listing 31.14 gives an example that displays a selected path or selected paths in tree. The user may select a node or multiple nodes and click the Show Path button to display the properties of the first selected path or the Show Paths button to display all the selected paths in a text area, as shown in Figure 31.25. The Show Path button displays a path from the last node up to the root.
Figure 31.25. The selected path(s) are processed .
Listing 31.14. TestTreePath.java
(This item is displayed on pages 1079 - 1080 in the print version)
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 import javax.swing.tree.*; 5 6 public class TestTreePath extends JApplet { 7 private JTree jTree = new JTree(); 8 private JTextArea jtaOutput = new JTextArea(); 9 private JButton jbtShowPath = new JButton( "Show Path" ); 10 private JButton jbtShowPaths = new JButton( "Show Paths" ); 11 12 public TestTreePath() { 13 JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 14 new JScrollPane(jTree), new JScrollPane(jtaOutput)); 15 16 JPanel panel = new JPanel(); 17 panel.add(jbtShowPath); 18 panel.add(jbtShowPaths); 19 20 add(splitPane, BorderLayout.CENTER); 21 add(panel, BorderLayout.NORTH); 22 23 jbtShowPath.addActionListener( new ActionListener() { 24 public void actionPerformed(ActionEvent e) { 25 TreePath path = jTree.getSelectionPath(); 26 jtaOutput.append( "\nProcessing a single path\n" ); 27 jtaOutput.append( "# of elements: " + path.getPathCount() ); 28 jtaOutput.append( "\nlast element: " 29 + path.getLastPathComponent() ); 30 jtaOutput.append( "\nfrom last node in the path to the root: " ); 31 TreeNode node = (TreeNode)path.getLastPathComponent(); 32 while (node != null ) { 33 jtaOutput.append(node.toString() + " " ); 34 node = node.getParent(); 35 } 36 }}); 37 38 jbtShowPaths.addActionListener( new ActionListener() { 39 public void actionPerformed(ActionEvent e) { 40 jtaOutput.append( "\nProcessing multiple paths\n" ); 41 javax.swing.tree.TreePath[] paths = jTree.getSelectionPaths(); 42 for ( int i = ; i < paths.length; i++) 43 jtaOutput.append( paths[i].toString() + "\n" ); 44 }}); 45 } 46 }
|
The getSelectionPath() method invoked from a JTree returns a TreePath in line 25. The first node in the path is always the root of the tree. The getPathCount() invoked from a TreePath returns the number of nodes in the path (line 27). The getLastPathComponent() invoked from a TreePath returns the last node in the path (line 29). The return node type is Object . You need to cast it to a TreeNode (line 31) in order to invoke the getParent() method from a TreeNode (line 34).
While the getSelectionPath() method (line 25) returns the first selected path, the getSelectionPaths() method (line 41) returns all the selected paths in an array of paths.