Home » openjdk-7 » javax » swing » tree » [javadoc | source]
javax.swing.tree
public class: TreePath [javadoc | source]
java.lang.Object
   javax.swing.tree.TreePath

All Implemented Interfaces:
    java$io$Serializable

{@code 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 {@code /tmp/foo/bar} could be represented by a {@code TreePath} as {@code new TreePath(new Object[] {"tmp", "foo", "bar"})}.

{@code TreePath} is used extensively by {@code JTree} and related classes. For example, {@code JTree} represents the selection as an array of {@code TreePath}s. When used with {@code JTree}, the elements of the path are the objects returned from the {@code TreeModel}. When {@code JTree} is paired with {@code DefaultTreeModel}, the elements of the path are {@code TreeNode}s. The following example illustrates extracting the user object from the selection of a {@code 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 {@code getLastPathComponent}, and {@code getParentPath}. As {@code JTree} internally creates {@code TreePath}s at various points, it's generally not useful to subclass {@code TreePath} and use with {@code JTree}.

While {@code TreePath} is serializable, a {@code 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 JavaBeansTM has been added to the java.beans package. Please see java.beans.XMLEncoder .

Constructor:
 protected TreePath() 
 public TreePath(Object[] path) 
    Creates a {@code TreePath} 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 - if {@code path} is {@code null}, empty, or contains a {@code null} value
 public TreePath(Object lastPathComponent) 
    Creates a {@code TreePath} containing a single element. This is used to construct a {@code TreePath} identifying the root.
    Parameters:
    lastPathComponent - the root
    Throws:
    IllegalArgumentException - if {@code lastPathComponent} is {@code null}
    Also see:
    TreePath(Object[])
 protected TreePath(TreePath parent,
    Object lastPathComponent) 
    Creates a {@code TreePath} with the specified parent and element.
    Parameters:
    parent - the path to the parent, or {@code null} to indicate the root
    lastPathComponent - the last path element
    Throws:
    IllegalArgumentException - if {@code lastPathComponent} is {@code null}
 protected TreePath(Object[] path,
    int length) 
    Creates a {@code TreePath} from an array. The returned {@code TreePath} represents the elements of the array from {@code 0} to {@code length - 1}.

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

    Parameters:
    path - the array to create the {@code TreePath} from
    length - identifies the number of elements in {@code path} to create the {@code TreePath} from
    Throws:
    NullPointerException - if {@code path} is {@code null}
    ArrayIndexOutOfBoundsException - if {@code length - 1} is outside the range of the array
    IllegalArgumentException - if any of the elements from {@code 0} to {@code length - 1} are {@code null}
Method from javax.swing.tree.TreePath Summary:
equals,   getLastPathComponent,   getParentPath,   getPath,   getPathComponent,   getPathCount,   hashCode,   isDescendant,   pathByAddingChild,   toString
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from javax.swing.tree.TreePath Detail:
 public boolean equals(Object o) 
    Compares this {@code TreePath} to the specified object. This returns {@code true} if {@code o} is a {@code TreePath} with the exact same elements (as determined by using {@code equals} on each element of the path).
 public Object getLastPathComponent() 
    Returns the last element of this path.
 public TreePath getParentPath() 
    Returns the {@code TreePath} of the parent. A return value of {@code null} indicates this is the root node.
 public Object[] getPath() 
    Returns an ordered array of the elements of this {@code TreePath}. The first element is the root.
 public Object getPathComponent(int index) 
    Returns the path element at the specified index.
 public int getPathCount() 
    Returns the number of elements in the path.
 public int hashCode() 
    Returns the hash code of this {@code TreePath}. The hash code of a {@code TreePath} is the hash code of the last element in the path.
 public boolean isDescendant(TreePath aTreePath) 
    Returns true if aTreePath is a descendant of this {@code TreePath}. A {@code TreePath} {@code P1} is a descendant of a {@code TreePath} {@code P2} if {@code P1} contains all of the elements that make up {@code P2's} path. For example, if this object has the path {@code [a, b]}, and aTreePath has the path {@code [a, b, c]}, then aTreePath is a descendant of this object. However, if aTreePath has the path {@code [a]}, then it is not a descendant of this object. By this definition a {@code TreePath} is always considered a descendant of itself. That is, aTreePath.isDescendant(aTreePath) returns {@code true}.
 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 {@code TreePath}.
 public String toString() 
    Returns a string that displays and identifies this object's properties.