In the first form, the tree is constructed from the elements in the array obj. The second form
constructs the tree from the elements of vector v. In the third form, the tree whose root node
is specified by tn specifies the tree.
Although JTree is packaged in javax.swing, its support classes and interfaces are
packaged in javax.swing.tree. This is because the number of classes and interfaces needed
to support JTree is quite large.
JTree relies on two models: TreeModel and TreeSelectionModel. A JTree generates a
variety of events, but three relate specifically to trees: TreeExpansionEvent,
TreeSelectionEvent, and TreeModelEvent. TreeExpansionEvent events occur when a node
is expanded or collapsed. A TreeSelectionEvent is generated when the user selects or
deselects a node within the tree. A TreeModelEvent is fired when the data or structure of the
tree changes. The listeners for these events are TreeExpansionListener, TreeSelectionListener,
and TreeModelListener, respectively. The tree event classes and listener interfaces are
packaged in javax.swing.event.
The event handled by the sample program shown in this section is TreeSelectionEvent.
To listen for this event, implement TreeSelectionListener. It defines only one method, called
valueChanged( ), which receives the TreeSelectionEvent object. You can obtain the path to
the selected object by calling getPath( ), shown here, on the event object.
TreePath getPath( )
It returns a TreePath object that describes the path to the changed node. The TreePath class
encapsulates information about a path to a particular node in a tree. It provides several
constructors and methods. In this topic, only the toString( ) method is used. It returns a
string that describes the path.
The TreeNode interface declares methods that obtain information about a tree node.
For example, it is possible to obtain a reference to the parent node or an enumeration of the
child nodes. The MutableTreeNode interface extends TreeNode. It declares methods that
can insert and remove child nodes or change the parent node.
The DefaultMutableTreeNode class implements the MutableTreeNode interface. It
represents a node in a tree. One of its constructors is shown here:
DefaultMutableTreeNode(Object obj)
Here, obj is the object to be enclosed in this tree node. The new tree node doesn't have a
parent or children.
To create a hierarchy of tree nodes, the add( ) method of DefaultMutableTreeNode can
be used. Its signature is shown here:
void add(MutableTreeNode child)
Here, child is a mutable tree node that is to be added as a child to the current node.
JTree does not provide any scrolling capabilities of its own. Instead, a JTree is typically
placed within a JScrollPane. This way, a large tree can be scrolled through a smaller viewport.
Here are the steps to follow to use a tree:
1. Create an instance of JTree.
2. Create a JScrollPane and specify the tree as the object to be scrolled.
3. Add the tree to the scroll pane.
4. Add the scroll pane to the content pane.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home