How to Write a Tree Selection Listener

 <  Day Day Up  >  

To detect when the user selects a node in a tree, you need to register a tree selection listener. Here's an example, taken from the TreeDemo example discussed in Responding to Node Selection (page 440) in Chapter 7, of detecting node selection in a tree that can have at most one node selected at a time:

 tree.addTreeSelectionListener(new TreeSelectionListener() {     public void valueChanged(TreeSelectionEvent e) {         DefaultMutableTreeNode node = (DefaultMutableTreeNode)                            tree.getLastSelectedPathComponent();         if (node == null) return;         Object nodeInfo = node.getUserObject();         ...         /* React to the node selection. */         ...     } }); 

To specify that the tree should support single selection, the program uses this code:

 tree.getSelectionModel().setSelectionMode         (TreeSelectionModel.SINGLE_TREE_SELECTION); 

The TreeSelectionModel interface defines three values for the selection mode:

DISCONTIGUOUS_TREE_SELECTION

This is the default mode for the default tree selection model. With this mode, any combination of nodes can be selected.

SINGLE_TREE_SELECTION

This is the mode used by the preceding example. At most one node can be selected at a time.

CONTIGUOUS_TREE_SELECTION

With this mode, only nodes in adjoining rows can be selected.

The Tree Selection Listener API

Table 39 lists the only method in the TreeSelectionListener interface and Table 40 describes the methods in the TreeExpansionEvent class. Note that TreeSelectionListener has no corresponding adapter class. Also refer to the TreeSelectionListener API documentation at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/TreeSelectionListener.html. The TreeExpansionEvent API documentation is online at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/TreeExpansionEvent.html.

Table 39. The TreeSelectionListener Interface

Method

Purpose

valueChanged(TreeSelectionEvent)

Called whenever the selection changes.

Table 40. The TreeExpansionEvent API

Method

Purpose

 Object getSource() (  in  java.util.EventObject) 

Return the object that fired the event.

TreePath getNewLeadSelectionPath()

Return the current lead path .

TreePath getOldLeadSelectionPath()

Return the path that was previously the lead path.

TreePath getPath()

Return the first path element.

TreePath[] getPaths()

Return the paths that have been added or removed from the selection.

boolean isAddedPath()

Return true if the first path element has been added to the selection. Return false if the first path has been removed from the selection.

boolean isAddedPath(int)

Return true if the path specified by the index was added to the selection.

boolean isAddedPath(TreePath)

Return true if the specified path was added to the selection.

Object getLastSelectedPathComponent()

Return the last path component in the first node of the current selection.

 TreePath getLeadSelectionPath() (  in  JTree) 

Return the current lead path.

Examples That Use Tree Selection Listeners

The following examples use tree selection listeners.

Example

Where Described

Notes

TreeDemo

How to Use Trees (page 437)

The tree listener responds to node clicks by showing the appropriate HTML document.

 <  Day Day Up  >  


JFC Swing Tutorial, The. A Guide to Constructing GUIs
The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
ISBN: 0201914670
EAN: 2147483647
Year: 2004
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net