Java Reference
In-Depth Information
Figure 17-20.
TreePath sample tree
Using Figure 17-20 to visualize a tree starting from its root, the
TreePath
for the
Jets
node
would be described by its properties as follows:
•
lastPathComponent
: A
DefaultMutableTreeNode
whose user object is
Jets
.
•
parentPath
: A
TreePath
made up of the
root
,
New York
, and
Football
nodes.
•
path
: An array of
DefaultMutableTreeNode
nodes whose user objects are
root
,
New York
,
Football
, and
Jets
.
•
pathCount
: 4.
That's really all there is to the
TreePath
class. Just remember that you can't change an
existing
TreePath
—you can only access its elements.
Additional Expansion Events
Two listeners that can be registered with a
JTree
have yet to be discussed: a
TreeExpansionListener
and a
TreeWillExpandListener
.
TreeExpansionListener Interface and TreeExpansionEvent Class
If you're interested in finding out when a tree node has been expanded or collapsed, you can
register a
TreeExpansionListener
with a
JTree
. Any registered listener will be notified after the
expansion or collapse of a parent node.
public interface TreeExpansionListener implements EventListener {
public void treeCollapse(TreeExpansionEvent treeExpansionEvent);
public void treeExpand(TreeExpansionEvent treeExpansionEvent);
}