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()); Object myObject= selectedNode.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
has been added to the java.beans
package.
Please see java.
.
Modifier and Type | Field and Description |
---|---|
private Object | lastPathComponent
Last path component. |
private TreePath | parentPath
Path representing the parent, null if lastPathComponent represents the root. |
Access | Constructor and Description |
---|---|
public | |
public | |
protected | |
protected | |
protected |
Modifier and Type | Method and Description |
---|---|
public boolean | equals(Object
the object to compare o)Overrides java. TreePath to the specified object.
|
public Object | |
public TreePath | |
public Object[] | Returns: an array of the elements in thisTreePath Returns an ordered array of the elements of this |
public Object | Returns: the element at the specified indexthe index of the element requested index)Returns the path element at the specified index. |
public int | Returns: the number of elements in the pathReturns the number of elements in the path. |
public int | Returns: the hashCode for the objectOverrides java. TreePath .
|
public boolean | Returns: true ifaTreePath is a descendant of this paththe aTreePath)TreePath to checkReturns true if |
public TreePath | Returns: a new path containing all the elements of this path pluschild the path element to add child)Returns a new path containing all the elements of this path
plus |
public String | Returns: a String representation of this objectOverrides java. |
lastPathComponent | back to summary |
---|---|
private Object lastPathComponent Last path component. |
parentPath | back to summary |
---|---|
private TreePath parentPath Path representing the parent, null if lastPathComponent represents the root. |
TreePath | back to summary |
---|---|
public TreePath(Object[] path) Creates a
|
TreePath | back to summary |
---|---|
public TreePath(Object lastPathComponent) Creates a
|
TreePath | back to summary |
---|---|
protected TreePath(TreePath parent, Object lastPathComponent) Creates a
|
TreePath | back to summary |
---|---|
protected TreePath(Object[] path, int length) Creates a This constructor is used internally, and generally not useful outside of subclasses.
|
TreePath | back to summary |
---|---|
protected TreePath() Creates an empty |
equals | back to summary |
---|---|
public boolean equals(Object o) Overrides java. Compares this
|
getLastPathComponent | back to summary |
---|---|
public Object getLastPathComponent() Returns the last element of this path.
|
getParentPath | back to summary |
---|---|
public TreePath getParentPath() Returns the
|
getPath | back to summary |
---|---|
public Object[] getPath() Returns an ordered array of the elements of this
|
getPathComponent | back to summary |
---|---|
public Object getPathComponent(int index) Returns the path element at the specified index.
|
getPathCount | back to summary |
---|---|
public int getPathCount() Returns the number of elements in the path.
|
hashCode | back to summary |
---|---|
public int hashCode() Overrides java. Returns the hash code of this
|
isDescendant | back to summary |
---|---|
public boolean isDescendant(TreePath aTreePath) Returns true if
|
pathByAddingChild | back to summary |
---|---|
public TreePath pathByAddingChild(Object child) Returns a new path containing all the elements of this path
plus
|
toString | back to summary |
---|---|
public String toString() Overrides java. Returns a string that displays and identifies this object's properties.
|