public class TreePath extends Object implements Serializable
TreePath表示唯一标识树中某个节点的路径的对象数组。
数组的元素以root为单位,作为数组的第一个元素。
例如,文件系统上的一个文件是基于父目录的数组和文件的名称来唯一标识的。
路径/tmp/foo/bar可以由TreePath为new TreePath(new Object[] {"tmp", "foo", "bar"}) 。
TreePath被广泛使用JTree和相关类。 例如, JTree表示选择作为阵列TreePath秒。 当与JTree一起使用时,路径的元素是从TreeModel返回的对象。 当JTree与DefaultTreeModel配对时,路径的元素为TreeNode s。 以下示例说明从选择JTree中JTree用户对象:
DefaultMutableTreeNode root = ...;
DefaultTreeModel model = new DefaultTreeModel(root);
JTree tree = new JTree(model);
...
TreePath selectedPath = tree.getSelectionPath();
DefaultMutableTreeNode selectedNode =
((DefaultMutableTreeNode)selectedPath.getLastPathComponent()).
getUserObject();
子类通常只需要覆盖getLastPathComponent和getParentPath 。
由于JTree内部创建了TreePath s的各个点,通常没有用的子类TreePath并与JTree使用。
虽然TreePath是可序列化的,但如果路径的任何元素NotSerializableException ,则会抛出NotSerializableException。
有关使用树路径的更多信息和示例,请参阅Java教程中的 How to Use Trees 。
警告:此类的序列化对象与将来的Swing版本不兼容。 当前的序列化支持适用于运行相同版本的Swing的应用程序之间的短期存储或RMI。 从1.4开始,对所有JavaBeans的长期存储的支持已经添加到java.beans包中。 请参阅XMLEncoder 。
| Modifier | Constructor and Description |
|---|---|
protected |
TreePath()
创建一个空的
TreePath 。
|
|
TreePath(Object lastPathComponent)
创建一个
TreePath单个元素的
TreePath 。
|
|
TreePath(Object[] path)
从数组创建一个
TreePath 。
|
protected |
TreePath(Object[] path, int length)
从数组创建一个
TreePath 。
|
protected |
TreePath(TreePath parent, Object lastPathComponent)
用指定的父元素和元素创建一个
TreePath 。
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object o)
将此
TreePath与指定对象进行比较。
|
Object |
getLastPathComponent()
返回此路径的最后一个元素。
|
TreePath |
getParentPath()
返回父母的
TreePath 。
|
Object[] |
getPath()
返回这个
TreePath的元素的有序数组。
|
Object |
getPathComponent(int index)
返回指定索引处的path元素。
|
int |
getPathCount()
返回路径中的元素数。
|
int |
hashCode()
返回此
TreePath的哈希码。
|
boolean |
isDescendant(TreePath aTreePath)
如果
aTreePath是这个
TreePath的后代,则返回true。
|
TreePath |
pathByAddingChild(Object child)
返回包含此路径的所有元素的新路径加上
child 。
|
String |
toString()
返回显示和标识此对象属性的字符串。
|
@ConstructorProperties(value="path") public TreePath(Object[] path)
TreePath 。
数组唯一标识节点的路径。
path - 表示节点路径的对象数组
IllegalArgumentException - 如果
path是
null ,为空,或包含一个
null值
public TreePath(Object lastPathComponent)
TreePath单个元素的TreePath 。
这是用来构造一个TreePath标识根。
lastPathComponent - 根
IllegalArgumentException - 如果
lastPathComponent是
null
TreePath(Object[])
protected TreePath(TreePath parent, Object lastPathComponent)
TreePath 。
parent - 到父级的路径,或
null表示根
lastPathComponent - 最后一个路径元素
IllegalArgumentException - 如果
lastPathComponent是
null
protected TreePath(Object[] path, int length)
TreePath 。
返回的TreePath表示阵列的元素从0到length - 1 。
这个构造函数在内部使用,一般在子类之外是无用的。
path - 数组创建
TreePath
length - 识别
path中创建
TreePath的元素数
NullPointerException - 如果
path是
null
ArrayIndexOutOfBoundsException - 如果
length - 1在数组的范围之外
IllegalArgumentException - 如果任何元素从
0到
length - 1是
null
protected TreePath()
TreePath 。
这是为以不同方式表示路径的子类提供的。
使用此构造函数的子类必须覆盖getLastPathComponent和getParentPath 。
public Object[] getPath()
TreePath的元素的有序数组。
第一个元素是根。
TreePath
public Object getLastPathComponent()
public int getPathCount()
public Object getPathComponent(int index)
index - 请求元素的索引
IllegalArgumentException - 如果索引超出此路径的范围
public boolean equals(Object o)
TreePath与指定对象进行比较。
如果o是一个TreePath ,具有完全相同的元素(由路径的每个元素使用equals ,则返回true 。
equals在
Object类
o - 要比较的对象
true如果此对象与obj参数相同;
false否则。
Object.hashCode() , HashMap
public int hashCode()
TreePath的哈希码。
TreePath的哈希码是路径中最后一个元素的哈希码。
hashCode在
Object
Object.equals(java.lang.Object) ,
System.identityHashCode(java.lang.Object)
public boolean isDescendant(TreePath aTreePath)
aTreePath是这个TreePath的后代,则返回true。
A TreePath P1是TreePath TreePath P2如果P1包含构成P2's路径的所有元素。
例如,如果此对象具有路径[a, b] ,并且aTreePath具有路径[a, b, c] ,那么aTreePath是此对象的后代。
但是,如果aTreePath具有路径[a] ,那么它不是此对象的后代。
通过这个定义,一个TreePath一直被认为是自己的后裔。
也就是说, aTreePath.isDescendant(aTreePath)返回true 。
aTreePath -
TreePath检查
aTreePath是此路径的后代,则为true
public TreePath pathByAddingChild(Object child)
child 。
child是新创建的TreePath的最后一个元素。
child - 要添加的路径元素
NullPointerException - 如果
child是
null
public TreePath getParentPath()
TreePath 。
返回值为null表示这是根节点。
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.