|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectjavax.swing.tree.DefaultMutableTreeNode
public class DefaultMutableTreeNode
DefaultMutableTreeNode 是树数据结构中的通用节点。有关使用默认可变树节点的示例,请参阅 The Java Tutorial 中的 How to Use Trees 一节。
一个树节点最多可以有一个父节点、0 或多个子节点。DefaultMutableTreeNode 为检查和修改节点的父节点和子节点提供操作,也为检查节点所属的树提供操作。节点的树是所有节点的集合,通过从某一节点开始并沿着父节点和子节点的所有可能的链接,可以访问这些节点。不带父节点的节点是树的根;不带子节点的节点是叶节点。树可以由许多子树组成,每个节点充当自已的子树的根。
此类提供按各种顺序有效地遍历树或子树,或者沿着两节点间的路径进行遍历的枚举。DefaultMutableTreeNode 还可以保存对用户对象的引用,用户对象由用户使用。通过 toString() 请求 DefaultMutableTreeNode 的字符串表示形式将返回其用户对象的字符串表示形式。
这不是一个线程安全的类。如果打算在多个线程中使用 DefaultMutableTreeNode(或 TreeNodes 的树),则需要执行自己的同步。要采用的较好惯例是在树的根节点上进行同步。
尽管 DefaultMutableTreeNode 实现 MutableTreeNode 接口,并允许您将其添加到 MutableTreeNode 的任何实现中,但是并不是 DefaultMutableTreeNode 中的所有方法都将适用于所有的 MutableTreeNodes 实现。特别是通过提供的一些枚举,使用其中一些方法,我们假定 DefaultMutableTreeNode 只包含 DefaultMutableNode 实例。无论添加什么实现,所有的 TreeNode/MutableTreeNode 方法都将表现出定义的行为。
警告:此类的已序列化对象与以后的 Swing 版本不兼容。当前的序列化支持适用于短期存储或运行相同 Swing 版本的应用程序之间的 RMI。从 1.4 版本开始,已在 java.beans 包中添加了支持所有 JavaBeansTM 长期存储的功能。请参见 XMLEncoder。
MutableTreeNode
| 字段摘要 | |
|---|---|
protected boolean |
allowsChildren 如果该节点可以有子节点,则为 true |
protected Vector |
children 子节点的数组,如果此节点没有子节点,则可以为 null |
static Enumeration<TreeNode> |
EMPTY_ENUMERATION 始终为空的枚举。 |
protected MutableTreeNode |
parent 此节点的父节点,如果此节点没有父节点,则为 null |
protected Object |
userObject 可选用户对象 |
| 构造方法摘要 | |
|---|---|
DefaultMutableTreeNode() 创建没有父节点和子节点的树节点,该树节点允许有子节点。 |
|
DefaultMutableTreeNode(Object userObject) 创建没有父节点和子节点、但允许有子节点的树节点,并使用指定的用户对象对它进行初始化。 |
|
DefaultMutableTreeNode(Object userObject, boolean allowsChildren) 创建没有父节点和子节点的树节点,使用指定的用户对象对它进行初始化,仅在指定时才允许有子节点。 |
|
| 方法摘要 | |
|---|---|
void |
add(MutableTreeNode newChild) 从其父节点移除 newChild,并通过将其添加到此节点的子数组的结尾,使其成为此节点的子节点。 |
Enumeration |
breadthFirstEnumeration() 创建并返回一个枚举,该枚举按广度优先的顺序遍历以此节点为根的子树。 |
Enumeration |
children() 创建并返回此节点的子节点的正向枚举。 |
Object |
clone() 重写以复制 public。 |
Enumeration |
depthFirstEnumeration() 创建并返回一个枚举,该枚举按深度优先的顺序遍历以此节点为根的子树。 |
boolean |
getAllowsChildren() 如果允许此节点拥有子节点,则返回 true。 |
TreeNode |
getChildAfter(TreeNode aChild) 返回此节点的子节点数组中刚好跟在 aChild 之后的子节点,它必须是此节点的子节点。 |
TreeNode |
getChildAt(int index) 返回此节点的子节点数组中指定索引处的子节点。 |
TreeNode |
getChildBefore(TreeNode aChild) 返回此节点的子节点数组中刚好在 aChild 之前的子节点,它必须是此节点的子节点。 |
int |
getChildCount() 返回此节点的子节点数。 |
int |
getDepth() 返回以此节点为根的树的深度 -- 从此节点到叶节点的最长距离。 |
TreeNode |
getFirstChild() 返回此节点的第一个子节点。 |
DefaultMutableTreeNode |
getFirstLeaf() 查找并返回为此节点后代的第一个叶节点,即此节点或其第一个子节点的第一个叶节点。 |
int |
getIndex(TreeNode aChild) 返回此节点的子节点数组中指定子节点的索引。 |
TreeNode |
getLastChild() 返回此节点的最后一个子节点。 |
DefaultMutableTreeNode |
getLastLeaf() 查找并返回为此节点后代的最后一个叶节点,即此节点或其最后一个子节点的最后一个叶节点。 |
int |
getLeafCount() 返回为此节点后代的叶节点总数。 |
int |
getLevel() 返回此节点上的级数 -- 从根到此节点的距离。 |
DefaultMutableTreeNode |
getNextLeaf() 返回此节点后面的叶节点,如果此节点是树中的最后一个叶节点,则返回 null。 |
DefaultMutableTreeNode |
getNextNode() 返回在此节点的树的前序遍历中此节点之后的节点。 |
DefaultMutableTreeNode |
getNextSibling() 返回父节点的子节点数组中此节点的下一个兄弟节点。 |
TreeNode |
getParent() 返回此节点的父节点,如果此节点没有父节点,则返回 null。 |
TreeNode[] |
getPath() 返回从根到达此节点的路径。 |
protected TreeNode[] |
getPathToRoot(TreeNode aNode, int depth) 构建节点的父节点一直到根节点(包括根节点),其中源节点是返回数组中的最后一个元素。 |
DefaultMutableTreeNode |
getPreviousLeaf() 返回此节点之前的叶节点,如果此节点是树中的第一个叶节点,则返回 null。 |
DefaultMutableTreeNode |
getPreviousNode() 返回在此节点树的前序遍历中此节点之前的节点。 |
DefaultMutableTreeNode |
getPreviousSibling() 返回父节点的子节点数组中此节点的前一个兄弟节点。 |
TreeNode |
getRoot() 返回包含此节点的树的根。 |
TreeNode |
getSharedAncestor(DefaultMutableTreeNode aNode) 返回此节点和 aNode 最近的共同祖先。 |
int |
getSiblingCount() 返回此节点的兄弟节点数。 |
Object |
getUserObject() 返回此节点的用户对象。 |
Object[] |
getUserObjectPath() 返回从根到达此节点的路径。 |
void |
insert(MutableTreeNode newChild, int childIndex) 从其现有父节点移除 newChild(如果它有父节点),将子节点的父节点设置为此节点,然后,将该子节点添加到此节点位于索引 childIndex 处的子节点数组。 |
boolean |
isLeaf() 如果此节点没有子节点,则返回 true。 |
boolean |
isNodeAncestor(TreeNode anotherNode) 如果 anotherNode 是此节点的祖先 -- 如果它是此节点、此节点的父节点或此节点的父节点的祖先,则返回 true。 |
boolean |
isNodeChild(TreeNode aNode) 如果 aNode 是此节点的子节点,则返回 true。 |
boolean |
isNodeDescendant(DefaultMutableTreeNode anotherNode) 如果 anotherNode 是此节点的后代 -- 如果它是此节点、此节点的一个子节点或此节点的一个子节点的后代,则返回 true。 |
boolean |
isNodeRelated(DefaultMutableTreeNode aNode) 当且仅当 aNode 位于与此节点相同的树中时,返回 true。 |
boolean |
isNodeSibling(TreeNode anotherNode) 如果 anotherNode 是此节点的兄弟节点(与此节点共享同一父节点),则返回 true。 |
boolean |
isRoot() 如果此节点是树的根,则返回 true。 |
Enumeration |
pathFromAncestorEnumeration(TreeNode ancestor) 创建并返回沿着从 ancestor 到此节点的路径的一个枚举。 |
Enumeration |
postorderEnumeration() 创建并返回按后序遍历以此节点为根的树的一个枚举。 |
Enumeration |
preorderEnumeration() 创建并返回按前序遍历以此节点为根的子树的一个枚举。 |
void |
remove(int childIndex) 从此节点的子节点中移除指定索引处的子节点,并将该节点的父节点设置为 null。 |
void |
remove(MutableTreeNode aChild) 通过向其提供一个 null 父节点,从此节点的子节点数组移除 aChild。 |
void |
removeAllChildren() 通过将其父节点设置为 null,移除此节点的所有子节点。 |
void |
removeFromParent() 通过向此节点提供一个 null 父节点,从树中移除以此节点为根的子树。 |
void |
setAllowsChildren(boolean allows) 确定是否允许此节点拥有子节点。 |
void |
setParent(MutableTreeNode newParent) 将此节点的父节点设置为 newParent,但不更改父节点的子节点数组。 |
void |
setUserObject(Object userObject) 将此节点的用户对象设置为 userObject。 |
String |
toString() 返回将 toString() 发送到此节点的用户对象的结果,如果此节点没有用户对象,则返回 null。 |
| 从类 java.lang.Object 继承的方法 |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| 字段详细信息 |
|---|
public static final Enumeration<TreeNode> EMPTY_ENUMERATION
protected MutableTreeNode parent
protected Vector children
protected transient Object userObject
protected boolean allowsChildren
| 构造方法详细信息 |
|---|
public DefaultMutableTreeNode()
public DefaultMutableTreeNode(Object userObject)
userObject - 用户提供的 Object,它构成节点的数据
public DefaultMutableTreeNode(Object userObject,
boolean allowsChildren)
userObject - 用户提供的 Object,它构成该节点的数据
allowsChildren - 如果为 true,则允许该节点有子节点,否则它始终为一个叶节点
| 方法详细信息 |
|---|
public void insert(MutableTreeNode newChild,
int childIndex)
newChild(如果它有父节点),将子节点的父节点设置为此节点,然后,将该子节点添加到此节点位于索引
childIndex 处的子节点数组。
newChild 不能为 null,也不能为此节点的祖先。
MutableTreeNode 中的
insert
newChild - 在此节点下插入的 MutableTreeNode
childIndex - 此节点的子节点数组中的索引,它是此节点插入的位置
ArrayIndexOutOfBoundsException - 如果
childIndex 超出边界
IllegalArgumentException - 如果
newChild 为 null,或者是此节点的祖先
IllegalStateException - 如果此节点不允许有子节点
isNodeDescendant(javax.swing.tree.DefaultMutableTreeNode)
public void remove(int childIndex)
MutableTreeNode。
MutableTreeNode 中的
remove
childIndex - 要移除的此节点的子节点数组中子节点的索引
ArrayIndexOutOfBoundsException - 如果
childIndex 超出边界
public void setParent(MutableTreeNode newParent)
newParent,但不更改父节点的子节点数组。从
insert() 和
remove() 调用此方法,以重新分配子节点的父节点,而不应从其他任何地方向它传递消息。
MutableTreeNode 中的
setParent
newParent - 此节点的新父节点
public TreeNode getParent()
TreeNode 中的
getParent
public TreeNode getChildAt(int index)
TreeNode 中的
getChildAt
index - 此节点的子节点数组中的一个索引
ArrayIndexOutOfBoundsException - 如果
index 超出边界
public int getChildCount()
TreeNode 中的
getChildCount
public int getIndex(TreeNode aChild)
-1。此方法执行线性搜索,该方法花费时间为 O(n),其中 n 表示子节点数。
TreeNode 中的
getIndex
aChild - 要在此节点的子节点中搜索的 TreeNode
-1
IllegalArgumentException - 如果
aChild 为 null
public Enumeration children()
TreeNode 中的
children
public void setAllowsChildren(boolean allows)
allows 为 false,则移除此节点的所有子节点。
注:默认情况下,节点允许有子节点。
allows - 如果允许此节点拥有子节点,则为 true
public boolean getAllowsChildren()
TreeNode 中的
getAllowsChildren
public void setUserObject(Object userObject)
userObject。
MutableTreeNode 中的
setUserObject
userObject - 构成此节点的用户指定数据的 Object
getUserObject(),
toString()
public Object getUserObject()
setUserObject(java.lang.Object),
toString()
public void removeFromParent()
MutableTreeNode 中的
removeFromParent
public void remove(MutableTreeNode aChild)
aChild。
MutableTreeNode 中的
remove
aChild - 要移除的此节点的子节点
IllegalArgumentException - 如果
aChild 为 null,或者不是此节点的子节点
public void removeAllChildren()
public void add(MutableTreeNode newChild)
newChild,并通过将其添加到此节点的子数组的结尾,使其成为此节点的子节点。
newChild - 作为此节点的子节点添加的节点
IllegalArgumentException - 如果
newChild 为 null
IllegalStateException - 如果此节点不允许拥有子节点
insert(javax.swing.tree.MutableTreeNode, int)
public boolean isNodeAncestor(TreeNode anotherNode)
anotherNode 是此节点的祖先 -- 如果它是此节点、此节点的父节点或此节点的父节点的祖先,则返回 true。(注意,节点被认为是它本身的祖先。)如果
anotherNode 为 null,则此方法返回 false。此操作至少花费时间 O(h),其中 h 表示根到此节点的距离。
anotherNode - 测试是否为此节点的祖先的节点
anotherNode 的后代,则返回 true
isNodeDescendant(javax.swing.tree.DefaultMutableTreeNode),
getSharedAncestor(javax.swing.tree.DefaultMutableTreeNode)
public boolean isNodeDescendant(DefaultMutableTreeNode anotherNode)
anotherNode 是此节点的后代 -- 如果它是此节点、此节点的一个子节点或此节点的一个子节点的后代,则返回 true。注意,节点被认为是它本身的后代。如果
anotherNode 为 null,则返回 false。此操作至少花费时间 O(h),其中 h 表示根到
anotherNode 的距离。
anotherNode - 测试是否为此节点的后代的节点
anotherNode 的祖先,则返回 true
isNodeAncestor(javax.swing.tree.TreeNode),
getSharedAncestor(javax.swing.tree.DefaultMutableTreeNode)
public TreeNode getSharedAncestor(DefaultMutableTreeNode aNode)
aNode 最近的共同祖先。如果不存在这样的祖先 -- 如果此节点和
aNode 位于不同的树中,或者
aNode 为 null,则返回 null。节点被认为是它本身的祖先。
aNode - 查找其共同祖先的节点
aNode 最近的共同祖先,如果没有,则返回 null
isNodeAncestor(javax.swing.tree.TreeNode),
isNodeDescendant(javax.swing.tree.DefaultMutableTreeNode)
public boolean isNodeRelated(DefaultMutableTreeNode aNode)
aNode 位于与此节点相同的树中时,返回 true。如果
aNode 为 null,则返回 false。
aNode 位于与此节点相同的树中,则返回 true;如果
aNode 为 null,则返回 false
getSharedAncestor(javax.swing.tree.DefaultMutableTreeNode),
getRoot()
public int getDepth()
getLevel(),因为它必须有效地遍历以此节点为根的整棵树。
getLevel()
public int getLevel()
getDepth()
public TreeNode[] getPath()
protected TreeNode[] getPathToRoot(TreeNode aNode,
int depth)
aNode - 获取其路径的 TreeNode
depth - 提供朝根的方向(通过递归调用)执行的步骤数的 int,用于衡量返回的数组的大小
public Object[] getUserObjectPath()
public TreeNode getRoot()
isNodeAncestor(javax.swing.tree.TreeNode)
public boolean isRoot()
public DefaultMutableTreeNode getNextNode()
preorderEnumeration()
public DefaultMutableTreeNode getPreviousNode()
null。这是遍历整棵树的一种低效方式;请改用枚举。
preorderEnumeration()
public Enumeration preorderEnumeration()
nextElement() 方法返回的第一个节点是此节点。
通过插入、移除或移动节点修改树可以使修改前创建的任何枚举无效。
postorderEnumeration()
public Enumeration postorderEnumeration()
nextElement() 方法返回的第一个节点是最左边的叶节点。这与深度优先遍历相同。
通过插入、移除或移动节点修改树可以使修改前创建的任何枚举无效。
depthFirstEnumeration(),
preorderEnumeration()
public Enumeration breadthFirstEnumeration()
nextElement() 方法返回的第一个节点是此节点。
通过插入、移除或移动节点修改树可以使修改前创建的任何枚举无效。
depthFirstEnumeration()
public Enumeration depthFirstEnumeration()
nextElement() 方法返回的第一个节点是最左边的叶节点。这与后序遍历相同。
通过插入、移除或移动节点修改树可以使修改前创建的任何枚举无效。
breadthFirstEnumeration(),
postorderEnumeration()
public Enumeration pathFromAncestorEnumeration(TreeNode ancestor)
ancestor 到此节点的路径的一个枚举。枚举的
nextElement() 方法首先返回
ancestor,然后返回作为此节点祖先的
ancestor 的子节点,依此类推,最后返回此节点。创建枚举所花费的时间是 O(m),其中 m 是此节点和
ancestor(包含)之间的节点数。每个
nextElement() 消息的花费都是 O(1)。
通过插入、移除或移动节点修改树可以使修改前创建的任何枚举无效。
IllegalArgumentException - 如果
ancestor 不是此节点的祖先
isNodeAncestor(javax.swing.tree.TreeNode),
isNodeDescendant(javax.swing.tree.DefaultMutableTreeNode)
public boolean isNodeChild(TreeNode aNode)
aNode 是此节点的子节点,则返回 true。如果
aNode 为 null,则此方法返回 false。
aNode 为此节点的子节点,则返回 true;如果
aNode 为 null,则返回 false
public TreeNode getFirstChild()
NoSuchElementException - 如果此节点没有子节点
public TreeNode getLastChild()
NoSuchElementException - 如果此节点没有子节点
public TreeNode getChildAfter(TreeNode aChild)
aChild 之后的子节点,它必须是此节点的子节点。如果
aChild 是最后一个子节点,则返回 null。此方法对此节点的子节点执行线性搜索,以查找
aChild ,并且花费时间为 O(n),其中 n 表示子节点数;要遍历整个子节点数组,请改用枚举。
aChild 之后
IllegalArgumentException - 如果
aChild 为 null,或者不是此节点的子节点
children
public TreeNode getChildBefore(TreeNode aChild)
aChild 之前的子节点,它必须是此节点的子节点。如果
aChild 是第一个子节点,则返回 null。此方法对此节点的子节点执行线性搜索,以查找
aChild ,并且花费时间为 O(n) ,其中 n 表示子节点数。
aChild 之前
IllegalArgumentException - 如果
aChild 为 null,或者不是此节点的子节点
public boolean isNodeSibling(TreeNode anotherNode)
anotherNode 是此节点的兄弟节点(与此节点共享同一父节点),则返回 true。节点是其本身的兄弟节点。如果
anotherNode 为 null,则返回 false。
anotherNode - 测试其是否为此节点的兄弟节点
anotherNode 是此节点的兄弟节点,则返回 true
public int getSiblingCount()
1)。
public DefaultMutableTreeNode getNextSibling()
children
public DefaultMutableTreeNode getPreviousSibling()
public boolean isLeaf()
getAllowsChildren 联合使用
TreeNode 中的
isLeaf
getAllowsChildren()
public DefaultMutableTreeNode getFirstLeaf()
isLeaf(),
isNodeDescendant(javax.swing.tree.DefaultMutableTreeNode)
public DefaultMutableTreeNode getLastLeaf()
isLeaf(),
isNodeDescendant(javax.swing.tree.DefaultMutableTreeNode)
public DefaultMutableTreeNode getNextLeaf()
在 MutableNode 接口的此实现中,此操作效率非常低。为了确定下一个节点,此方法首先在父节点的子节点列表中执行线性搜索,以便找到当前节点。
该实现使该操作适合于从已知位置开始的简短遍历。但是,要遍历树中的所有叶节点,您应使用 depthFirstEnumeration 来枚举树中的节点,并对每个节点执行 isLeaf 确定哪些是叶节点。
depthFirstEnumeration(),
isLeaf()
public DefaultMutableTreeNode getPreviousLeaf()
在 MutableNode 接口的此实现中,此操作效率非常低。为了确定前一个节点,此方法首先在父节点的子节点列表中执行线性搜索,以便找到当前节点。
该实现使得操作适合于从已知位置开始的简短遍历。但是,要遍历树中的所有叶节点,您应使用 depthFirstEnumeration 来枚举树中的节点,并对每个节点执行 isLeaf 确定哪些是叶节点。
depthFirstEnumeration(),
isLeaf()
public int getLeafCount()
1。此方法花费时间为 O(n),其中 n 表示此节点的后代数。
isNodeAncestor(javax.swing.tree.TreeNode)
public String toString()
toString() 发送到此节点的用户对象的结果,如果此节点没有用户对象,则返回 null。
Object 中的
toString
getUserObject()
public Object clone()
Object 中的
clone
Cloneable
|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。