- All Implemented Interfaces:
Serializable
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
TreePaths. 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 TreeNodes. 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 TreePaths 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
has been added to the java.beans package.
Please see XMLEncoder.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedTreePath()Creates an emptyTreePath.Creates aTreePathcontaining a single element.Creates aTreePathfrom an array.protectedCreates aTreePathfrom an array.protectedCreates aTreePathwith the specified parent and element. -
Method Summary
Modifier and TypeMethodDescriptionbooleanCompares thisTreePathto the specified object.Returns the last element of this path.Returns theTreePathof the parent.Object[]getPath()Returns an ordered array of the elements of thisTreePath.getPathComponent(int index) Returns the path element at the specified index.intReturns the number of elements in the path.inthashCode()Returns the hash code of thisTreePath.booleanisDescendant(TreePath aTreePath) Returns true ifaTreePathis a descendant of thisTreePath.pathByAddingChild(Object child) Returns a new path containing all the elements of this path pluschild.toString()Returns a string that displays and identifies this object's properties.
-
Constructor Details
-
TreePath
Creates aTreePathfrom an array. The array uniquely identifies the path to a node.- Parameters:
path- an array of objects representing the path to a node- Throws:
IllegalArgumentException- ifpathisnull, empty, or contains anullvalue
-
TreePath
Creates aTreePathcontaining a single element. This is used to construct aTreePathidentifying the root.- Parameters:
lastPathComponent- the root- Throws:
IllegalArgumentException- iflastPathComponentisnull- See Also:
-
TreePath
Creates aTreePathwith the specified parent and element.- Parameters:
parent- the path to the parent, ornullto indicate the rootlastPathComponent- the last path element- Throws:
IllegalArgumentException- iflastPathComponentisnull
-
TreePath
Creates aTreePathfrom an array. The returnedTreePathrepresents the elements of the array from0tolength - 1.This constructor is used internally, and generally not useful outside of subclasses.
- Parameters:
path- the array to create theTreePathfromlength- identifies the number of elements inpathto create theTreePathfrom- Throws:
NullPointerException- ifpathisnullArrayIndexOutOfBoundsException- iflength - 1is outside the range of the arrayIllegalArgumentException- if any of the elements from0tolength - 1arenull
-
TreePath
protected TreePath()Creates an emptyTreePath. This is provided for subclasses that represent paths in a different manner. Subclasses that use this constructor must overridegetLastPathComponent, andgetParentPath.
-
-
Method Details
-
getPath
Returns an ordered array of the elements of thisTreePath. The first element is the root.- Returns:
- an array of the elements in this
TreePath
-
getLastPathComponent
Returns the last element of this path.- Returns:
- the last element in the path
-
getPathCount
public int getPathCount()Returns the number of elements in the path.- Returns:
- the number of elements in the path
-
getPathComponent
Returns the path element at the specified index.- Parameters:
index- the index of the element requested- Returns:
- the element at the specified index
- Throws:
IllegalArgumentException- if the index is outside the range of this path
-
equals
Compares thisTreePathto the specified object. This returnstrueifois aTreePathwith the exact same elements (as determined by usingequalson each element of the path). -
hashCode
public int hashCode()Returns the hash code of thisTreePath. The hash code of aTreePathis the hash code of the last element in the path. -
isDescendant
Returns true ifaTreePathis a descendant of thisTreePath. ATreePathP1is a descendant of aTreePathP2ifP1contains all of the elements that make upP2'spath. For example, if this object has the path[a, b], andaTreePathhas the path[a, b, c], thenaTreePathis a descendant of this object. However, ifaTreePathhas the path[a], then it is not a descendant of this object. By this definition aTreePathis always considered a descendant of itself. That is,aTreePath.isDescendant(aTreePath)returnstrue.- Parameters:
aTreePath- theTreePathto check- Returns:
- true if
aTreePathis a descendant of this path
-
pathByAddingChild
Returns a new path containing all the elements of this path pluschild.childis the last element of the newly createdTreePath.- Parameters:
child- the path element to add- Returns:
- a new path containing all the elements of this path
plus
child - Throws:
NullPointerException- ifchildisnull
-
getParentPath
Returns theTreePathof the parent. A return value ofnullindicates this is the root node.- Returns:
- the parent path
-
toString
Returns a string that displays and identifies this object's properties.
-