public class TreePath extends Object implements Serializable
TreePath代表对象的唯一标识路径树中的节点数组。数组的元素被排列为数组的第一个元素的根。例如,基于父目录数组和文件名的基础上唯一标识的文件系统上的文件是唯一标识的。路径
/tmp/foo/bar可以通过
TreePath表示为
new TreePath(new Object[] {"tmp", "foo", "bar"})。
TreePath由JTree广泛应用和相关的类。例如,JTree代表的选择作为TreePaths数组。当使用JTree,路径的元素是对象从TreeModel返回。当JTree搭配DefaultTreeModel,路径的元素TreeNodes。下面的例子说明了如何从一个JTree选择提取用户对象:
defaultmutabletreenode根=…;defaulttreemodel模式=新的defaulttreemodel(根);JTree树=新的树形结构(模型);…TreePath selectedPath =树。getselectionpath();DefaultMutableTreeNode selectedNode =((defaultmutabletreenode)selectedpath。getlastpathcomponent())。getuserobject();子类通常需要重写只有
getLastPathComponent,和
getParentPath。作为
JTree内部创建了在各点
TreePaths,这通常不是继承
TreePath使用
JTree有用。
而TreePath是可序列化的,一NotSerializableException如果路径的任意元素是不可序列化的扔。
用树路径的进一步信息和例子,看到在java教程How to Use Trees。
警告:序列化该类的对象与以后的Swing版本不兼容。当前的序列化支持适用于短期贮藏或RMI运行相同Swing版本的应用程序之间。为1.4,为所有JavaBeans™长期存储的支持已被添加到java.beans包。请看XMLEncoder。
| Modifier | Constructor and Description |
|---|---|
protected |
TreePath()
创建一个空的
TreePath。
|
|
TreePath(Object lastPathComponent)
创建一个
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)
返回指定索引处的路径元素。
|
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标识根。
lastPathComponent -根
null
lastPathComponent
IllegalArgumentException
TreePath(Object[])
protected TreePath(TreePath parent, Object lastPathComponent)
TreePath。
parent -路径的父,或
null表示根
lastPathComponent -最后的路径元素
null
lastPathComponent
IllegalArgumentException
protected TreePath(Object[] path, int length)
TreePath。返回的
TreePath表示从
length - 1
0到数组的元素。
此构造函数内部使用,一般不在子类外部使用。
path -数组创建
TreePath从
length标识元素在
path数量创造
TreePath从
null
path
NullPointerException
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到指定的对象。这还
true如果
o是具有完全相同的元素
TreePath(利用
equals路径上各元素的测定)。
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,
TreePath
P1是一个
TreePath
P2后代如果
P1包含所有构成元素
P2's路径。例如,如果该对象的路径
[a, b],和
aTreePath具有路径
[a, b, c],然后
aTreePath是这一对象的后代。然而,如果
aTreePath具有路径
[a],那它就不是一个该对象的后裔。按照这一定义,
TreePath总是认为本身的后裔。那是,
aTreePath.isDescendant(aTreePath)返回
true。
aTreePath -
TreePath检查
aTreePath是这一路径的后裔
public TreePath pathByAddingChild(Object child)
child元素。
child是新创建的最后一个元素
TreePath。
child -路径元素添加
null
child
NullPointerException
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.