- java.lang.Object
-
- javax.swing.tree.TreePath
-
- All Implemented Interfaces:
Serializable
public class TreePath extends Object implements 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 aTreePath
asnew TreePath(new Object[] {"tmp", "foo", "bar"})
.TreePath
is used extensively byJTree
and related classes. For example,JTree
represents the selection as an array ofTreePath
s. When used withJTree
, the elements of the path are the objects returned from theTreeModel
. WhenJTree
is paired withDefaultTreeModel
, the elements of the path areTreeNode
s. The following example illustrates extracting the user object from the selection of aJTree
: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 onlygetLastPathComponent
, andgetParentPath
. AsJTree
internally createsTreePath
s at various points, it's generally not useful to subclassTreePath
and use withJTree
.While
TreePath
is serializable, aNotSerializableException
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 seeXMLEncoder
.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
TreePath()
Creates an emptyTreePath
.TreePath(Object lastPathComponent)
Creates aTreePath
containing a single element.TreePath(Object[] path)
Creates aTreePath
from an array.protected
TreePath(Object[] path, int length)
Creates aTreePath
from an array.protected
TreePath(TreePath parent, Object lastPathComponent)
Creates aTreePath
with the specified parent and element.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object o)
Compares thisTreePath
to the specified object.Object
getLastPathComponent()
Returns the last element of this path.TreePath
getParentPath()
Returns theTreePath
of the parent.Object[]
getPath()
Returns an ordered array of the elements of thisTreePath
.Object
getPathComponent(int index)
Returns the path element at the specified index.int
getPathCount()
Returns the number of elements in the path.int
hashCode()
Returns the hash code of thisTreePath
.boolean
isDescendant(TreePath aTreePath)
Returns true ifaTreePath
is a descendant of thisTreePath
.TreePath
pathByAddingChild(Object child)
Returns a new path containing all the elements of this path pluschild
.String
toString()
Returns a string that displays and identifies this object's properties.
-
-
-
Constructor Detail
-
TreePath
@ConstructorProperties("path") public TreePath(Object[] path)
Creates aTreePath
from 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
- ifpath
isnull
, empty, or contains anull
value
-
TreePath
public TreePath(Object lastPathComponent)
Creates aTreePath
containing a single element. This is used to construct aTreePath
identifying the root.- Parameters:
lastPathComponent
- the root- Throws:
IllegalArgumentException
- iflastPathComponent
isnull
- See Also:
TreePath(Object[])
-
TreePath
protected TreePath(TreePath parent, Object lastPathComponent)
Creates aTreePath
with the specified parent and element.- Parameters:
parent
- the path to the parent, ornull
to indicate the rootlastPathComponent
- the last path element- Throws:
IllegalArgumentException
- iflastPathComponent
isnull
-
TreePath
protected TreePath(Object[] path, int length)
Creates aTreePath
from an array. The returnedTreePath
represents the elements of the array from0
tolength - 1
.This constructor is used internally, and generally not useful outside of subclasses.
- Parameters:
path
- the array to create theTreePath
fromlength
- identifies the number of elements inpath
to create theTreePath
from- Throws:
NullPointerException
- ifpath
isnull
ArrayIndexOutOfBoundsException
- iflength - 1
is outside the range of the arrayIllegalArgumentException
- if any of the elements from0
tolength - 1
arenull
-
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 Detail
-
getPath
public Object[] 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
public Object 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
public Object getPathComponent(int index)
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
public boolean equals(Object o)
Compares thisTreePath
to the specified object. This returnstrue
ifo
is aTreePath
with the exact same elements (as determined by usingequals
on each element of the path).- Overrides:
equals
in classObject
- Parameters:
o
- the object to compare- Returns:
true
if this object is the same as the obj argument;false
otherwise.- See Also:
Object.hashCode()
,HashMap
-
hashCode
public int hashCode()
Returns the hash code of thisTreePath
. The hash code of aTreePath
is the hash code of the last element in the path.- Overrides:
hashCode
in classObject
- Returns:
- the hashCode for the object
- See Also:
Object.equals(java.lang.Object)
,System.identityHashCode(java.lang.Object)
-
isDescendant
public boolean isDescendant(TreePath aTreePath)
Returns true ifaTreePath
is a descendant of thisTreePath
. ATreePath
P1
is a descendant of aTreePath
P2
ifP1
contains all of the elements that make upP2's
path. For example, if this object has the path[a, b]
, andaTreePath
has the path[a, b, c]
, thenaTreePath
is a descendant of this object. However, ifaTreePath
has the path[a]
, then it is not a descendant of this object. By this definition aTreePath
is always considered a descendant of itself. That is,aTreePath.isDescendant(aTreePath)
returnstrue
.- Parameters:
aTreePath
- theTreePath
to check- Returns:
- true if
aTreePath
is a descendant of this path
-
pathByAddingChild
public TreePath pathByAddingChild(Object child)
Returns a new path containing all the elements of this path pluschild
.child
is 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
- ifchild
isnull
-
getParentPath
public TreePath getParentPath()
Returns theTreePath
of the parent. A return value ofnull
indicates this is the root node.- Returns:
- the parent path
-
-