Top Description Fields Constructors Methods
javax.swing.tree

public Class TreePath

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@SuppressWarnings:serial
Imports
java.io.*, java.beans.ConstructorProperties

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());
  Object myObject= selectedNode.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 java.beans.XMLEncoder.

Authors
Scott Violet, Philip Milne

Field Summary

Modifier and TypeField and Description
private Object
lastPathComponent

Last path component.

private TreePath
parentPath

Path representing the parent, null if lastPathComponent represents the root.

Constructor Summary

AccessConstructor and Description
public
TreePath(Object[]
an array of objects representing the path to a node
path
)

Creates a TreePath from an array.

public
TreePath(Object
the root
lastPathComponent
)

Creates a TreePath containing a single element.

protected
TreePath(TreePath
the path to the parent, or null to indicate the root
parent
,
Object
the last path element
lastPathComponent
)

Creates a TreePath with the specified parent and element.

protected
TreePath(Object[]
the array to create the TreePath from
path
,
int
identifies the number of elements in path to create the TreePath from
length
)

Creates a TreePath from an array.

protected
TreePath()

Creates an empty TreePath.

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the object to compare
o
)

Overrides java.lang.Object.equals.

Compares this TreePath to the specified object.
public Object

Returns:

the last element in the path
getLastPathComponent
()

Returns the last element of this path.

public TreePath

Returns:

the parent path
getParentPath
()

Returns the TreePath of the parent.

public Object[]

Returns:

an array of the elements in this TreePath
getPath
()

Returns an ordered array of the elements of this TreePath.

public Object

Returns:

the element at the specified index
getPathComponent
(int
the index of the element requested
index
)

Returns the path element at the specified index.

public int

Returns:

the number of elements in the path
getPathCount
()

Returns the number of elements in the path.

public int

Returns:

the hashCode for the object
hashCode
()

Overrides java.lang.Object.hashCode.

Returns the hash code of this TreePath.
public boolean

Returns:

true if aTreePath is a descendant of this path
isDescendant
(TreePath
the TreePath to check
aTreePath
)

Returns true if aTreePath is a descendant of this TreePath.

public TreePath

Returns:

a new path containing all the elements of this path plus child
pathByAddingChild
(Object
the path element to add
child
)

Returns a new path containing all the elements of this path plus child.

public String

Returns:

a String representation of this object
toString
()

Overrides java.lang.Object.toString.

Returns a string that displays and identifies this object's properties.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

lastPathComponentback to summary
private Object lastPathComponent

Last path component.

parentPathback to summary
private TreePath parentPath

Path representing the parent, null if lastPathComponent represents the root.

Constructor Detail

TreePathback to summary
public TreePath(Object[] path)

Creates a TreePath from an array. The array uniquely identifies the path to a node.

Parameters
path:Object[]

an array of objects representing the path to a node

Annotations
@ConstructorProperties:path
Exceptions
IllegalArgumentException:
if path is null, empty, or contains a null value
TreePathback to summary
public TreePath(Object lastPathComponent)

Creates a TreePath containing a single element. This is used to construct a TreePath identifying the root.

Parameters
lastPathComponent:Object

the root

Exceptions
IllegalArgumentException:
if lastPathComponent is null
See Also
TreePath(Object[])
TreePathback to summary
protected TreePath(TreePath parent, Object lastPathComponent)

Creates a TreePath with the specified parent and element.

Parameters
parent:TreePath

the path to the parent, or null to indicate the root

lastPathComponent:Object

the last path element

Exceptions
IllegalArgumentException:
if lastPathComponent is null
TreePathback to summary
protected TreePath(Object[] path, int length)

Creates a TreePath from an array. The returned TreePath represents the elements of the array from 0 to length - 1.

This constructor is used internally, and generally not useful outside of subclasses.

Parameters
path:Object[]

the array to create the TreePath from

length:int

identifies the number of elements in path to create the TreePath from

Exceptions
NullPointerException:
if path is null
ArrayIndexOutOfBoundsException:
if length - 1 is outside the range of the array
IllegalArgumentException:
if any of the elements from 0 to length - 1 are null
TreePathback to summary
protected TreePath()

Creates an empty TreePath. This is provided for subclasses that represent paths in a different manner. Subclasses that use this constructor must override getLastPathComponent, and getParentPath.

Method Detail

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Compares this TreePath to the specified object. This returns true if o is a TreePath with the exact same elements (as determined by using equals on each element of the path).

Parameters
o:Object

the object to compare

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

getLastPathComponentback to summary
public Object getLastPathComponent()

Returns the last element of this path.

Returns:Object

the last element in the path

getParentPathback to summary
public TreePath getParentPath()

Returns the TreePath of the parent. A return value of null indicates this is the root node.

Returns:TreePath

the parent path

getPathback to summary
public Object[] getPath()

Returns an ordered array of the elements of this TreePath. The first element is the root.

Returns:Object[]

an array of the elements in this TreePath

getPathComponentback to summary
public Object getPathComponent(int index)

Returns the path element at the specified index.

Parameters
index:int

the index of the element requested

Returns:Object

the element at the specified index

Exceptions
IllegalArgumentException:
if the index is outside the range of this path
getPathCountback to summary
public int getPathCount()

Returns the number of elements in the path.

Returns:int

the number of elements in the path

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns the hash code of this TreePath. The hash code of a TreePath is the hash code of the last element in the path.

Returns:int

the hashCode for the object

isDescendantback to summary
public boolean isDescendant(TreePath aTreePath)

Returns true if aTreePath is a descendant of this TreePath. A TreePath P1 is a descendant of a TreePath P2 if P1 contains all of the elements that make up P2's path. For example, if this object has the path [a, b], and aTreePath has the path [a, b, c], then aTreePath is a descendant of this object. However, if aTreePath has the path [a], then it is not a descendant of this object. By this definition a TreePath is always considered a descendant of itself. That is, aTreePath.isDescendant(aTreePath) returns true.

Parameters
aTreePath:TreePath

the TreePath to check

Returns:boolean

true if aTreePath is a descendant of this path

pathByAddingChildback to summary
public TreePath pathByAddingChild(Object child)

Returns a new path containing all the elements of this path plus child. child is the last element of the newly created TreePath.

Parameters
child:Object

the path element to add

Returns:TreePath

a new path containing all the elements of this path plus child

Exceptions
NullPointerException:
if child is null
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string that displays and identifies this object's properties.

Returns:String

a String representation of this object