TreePath
represents an array of objects that uniquely identify the path to a node in a tree. The elements of the array are ordered with the root as the first element of the array. For example, a file on the file system is uniquely identified based on the array of parent directories and the name of the file. The path
/tmp/foo/bar
could be represented by a
TreePath
as
new TreePath(new Object[] {"tmp", "foo", "bar"})
.
TreePath
is used extensively by JTree
and related classes. For example, JTree
represents the selection as an array of TreePath
s. When used with JTree
, the elements of the path are the objects returned from the TreeModel
. When JTree
is paired with DefaultTreeModel
, the elements of the path are TreeNode
s. The following example illustrates extracting the user object from the selection of a JTree
:
DefaultMutableTreeNode root = ...;
DefaultTreeModel model = new DefaultTreeModel(root);
JTree tree = new JTree(model);
...
TreePath selectedPath = tree.getSelectionPath();
DefaultMutableTreeNode selectedNode =
((DefaultMutableTreeNode)selectedPath.getLastPathComponent()).
getUserObject();
Subclasses typically need override only
getLastPathComponent
, and
getParentPath
. As
JTree
internally creates
TreePath
s at various points, it's generally not useful to subclass
TreePath
and use with
JTree
.
While TreePath
is serializable, a NotSerializableException
is thrown if any elements of the path are not serializable.
For further information and examples of using tree paths, see How to Use Trees in The Java Tutorial.
Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans™JavaBeans has been added to the java.beans
package. Please see XMLEncoder
.