public class TreePath extends Object implements Serializable
TreePath
代表对象的唯一标识路径树中的节点数组。数组的元素被排列为数组的第一个元素的根。例如,基于父目录数组和文件名的基础上唯一标识的文件系统上的文件是唯一标识的。路径
/tmp/foo/bar
可以通过
TreePath
表示为
new TreePath(new Object[] {"tmp", "foo", "bar"})
。
TreePath
由JTree
广泛应用和相关的类。例如,JTree
代表的选择作为TreePath
s数组。当使用JTree
,路径的元素是对象从TreeModel
返回。当JTree
搭配DefaultTreeModel
,路径的元素TreeNode
s。下面的例子说明了如何从一个JTree
选择提取用户对象:
defaultmutabletreenode根=…;defaulttreemodel模式=新的defaulttreemodel(根);JTree树=新的树形结构(模型);…TreePath selectedPath =树。getselectionpath();DefaultMutableTreeNode selectedNode =((defaultmutabletreenode)selectedpath。getlastpathcomponent())。getuserobject();子类通常需要重写只有
getLastPathComponent
,和
getParentPath
。作为
JTree
内部创建了在各点
TreePath
s,这通常不是继承
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.