Module java.desktop
Package javax.swing.tree

Class DefaultMutableTreeNode

java.lang.Object
javax.swing.tree.DefaultMutableTreeNode
所有已实现的接口:
Serializable, Cloneable, MutableTreeNode, TreeNode
直接已知的子类:
JTree.DynamicUtilTreeNode

public class DefaultMutableTreeNode extends Object implements Cloneable, MutableTreeNode, Serializable
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 版开始,已将所有 JavaBeans 的长期存储支持添加到 java.beans 包中。请参阅XMLEncoder

参见:
  • Field Details

    • EMPTY_ENUMERATION

      public static final Enumeration<TreeNode> EMPTY_ENUMERATION
      An enumeration that is always empty. This is used when an enumeration of a leaf node's children is requested.
    • parent

      protected MutableTreeNode parent
      this node's parent, or null if this node has no parent
    • children

      protected Vector<TreeNode> children
      array of children, may be null if this node has no children
    • userObject

      protected transient Object userObject
      optional user object
    • allowsChildren

      protected boolean allowsChildren
      true if the node is able to have children
  • Constructor Details

    • DefaultMutableTreeNode

      public DefaultMutableTreeNode()
      Creates a tree node that has no parent and no children, but which allows children.
    • DefaultMutableTreeNode

      public DefaultMutableTreeNode(Object userObject)
      Creates a tree node with no parent, no children, but which allows children, and initializes it with the specified user object.
      Parameters:
      userObject - an Object provided by the user that constitutes the node's data
    • DefaultMutableTreeNode

      public DefaultMutableTreeNode(Object userObject, boolean allowsChildren)
      Creates a tree node with no parent, no children, initialized with the specified user object, and that allows children only if specified.
      Parameters:
      userObject - an Object provided by the user that constitutes the node's data
      allowsChildren - if true, the node is allowed to have child nodes -- otherwise, it is always a leaf node
  • Method Details

    • insert

      public void insert(MutableTreeNode newChild, int childIndex)
      Removes newChild from its present parent (if it has a parent), sets the child's parent to this node, and then adds the child to this node's child array at index childIndex. newChild must not be null and must not be an ancestor of this node.
      指定者:
      insert 在接口 MutableTreeNode
      参数:
      newChild - 要插入在此节点下方的MutableTreeNode
      childIndex - 要插入的节点在此节点的子节点数组中的索引
      抛出:
      ArrayIndexOutOfBoundsException - 如果childIndex超出范围
      IllegalArgumentException - 如果newChild为null或是此节点的祖先
      IllegalStateException - 如果此节点不允许有子节点
      参见:
    • remove

      public void remove(int childIndex)
      从此节点的子节点中删除指定索引处的子节点,并将该节点的父节点设置为null。要删除的子节点必须是一个MutableTreeNode
      指定者:
      remove 在接口 MutableTreeNode
      参数:
      childIndex - 要移除的子节点在此节点的子节点数组中的索引
      抛出:
      ArrayIndexOutOfBoundsException - 如果childIndex超出范围
    • setParent

      public void setParent(MutableTreeNode newParent)
      将此节点的父节点设置为newParent,但不更改父节点的子节点数组。此方法从insert()remove()中调用以重新分配子节点的父节点,不应该从其他任何地方调用。
      指定者:
      setParent 在接口 MutableTreeNode
      参数:
      newParent - 此节点的新父节点
    • getParent

      public TreeNode getParent()
      返回此节点的父节点,如果此节点没有父节点则返回null。
      指定者:
      getParent 在接口 TreeNode
      返回:
      此节点的父节点TreeNode,如果此节点没有父节点则返回null
    • getChildAt

      public TreeNode getChildAt(int index)
      返回此节点子节点数组中指定索引处的子节点。
      指定者:
      getChildAt 在接口 TreeNode
      参数:
      index - 此节点子节点数组中的索引
      返回:
      此节点子节点数组中指定索引处的TreeNode
      抛出:
      ArrayIndexOutOfBoundsException - 如果index超出范围
    • getChildCount

      public int getChildCount()
      返回此节点的子节点数量。
      指定者:
      getChildCount 在接口 TreeNode
      返回:
      一个int,表示此节点的子节点数量
    • getIndex

      public int getIndex(TreeNode aChild)
      返回此节点子节点数组中指定子节点的索引。如果指定的节点不是此节点的子节点,则返回-1。此方法执行线性搜索,时间复杂度为O(n),其中n为子节点数量。
      指定者:
      getIndex 在接口 TreeNode
      参数:
      aChild - 在此节点的子节点中搜索的TreeNode
      返回:
      一个int,表示节点在此节点的子节点数组中的索引,如果指定的节点不是此节点的子节点则返回-1
      抛出:
      IllegalArgumentException - 如果aChild为null
    • children

      public Enumeration<TreeNode> children()
      创建并返回此节点子节点的正向顺序枚举。修改此节点的子节点数组会使得任何在修改之前创建的子节点枚举无效。
      指定者:
      children 在接口 TreeNode
      返回:
      一个此节点子节点的枚举
    • setAllowsChildren

      public void setAllowsChildren(boolean allows)
      确定此节点是否允许有子节点。如果allows为false,则移除所有此节点的子节点。

      注意:默认情况下,节点允许有子节点。

      参数:
      allows - 如果此节点允许有子节点则为true
    • getAllowsChildren

      public boolean getAllowsChildren()
      如果此节点允许有子节点则返回true。
      指定者:
      getAllowsChildren 在接口 TreeNode
      返回:
      如果此节点允许有子节点则返回true,否则返回false
    • setUserObject

      public void setUserObject(Object userObject)
      将此节点的用户对象设置为userObject
      指定者:
      setUserObject 在接口 MutableTreeNode
      参数:
      userObject - 构成此节点用户指定数据的对象
      参见:
    • getUserObject

      public Object getUserObject()
      返回此节点的用户对象。
      返回:
      用户存储在此节点的对象
      参见:
    • removeFromParent

      public void removeFromParent()
      从树中移除以此节点为根的子树,使此节点的父节点为null。如果此节点是其树的根节点,则不执行任何操作。
      指定者:
      removeFromParent 在接口 MutableTreeNode
    • remove

      public void remove(MutableTreeNode aChild)
      从此节点的子节点数组中移除aChild,使其父节点为null。
      指定者:
      remove 在接口 MutableTreeNode
      参数:
      aChild - 要移除的此节点的子节点
      抛出:
      IllegalArgumentException - 如果aChild为null或不是此节点的子节点
    • removeAllChildren

      public void removeAllChildren()
      移除所有此节点的子节点,将它们的父节点设置为null。如果此节点没有子节点,则此方法不执行任何操作。
    • add

      public void add(MutableTreeNode newChild)
      从其父节点中移除newChild,并通过将其添加到此节点的子节点数组的末尾使其成为此节点的子节点。
      参数:
      newChild - 要作为此节点子节点添加的节点
      抛出:
      IllegalArgumentException - 如果newChild为null
      IllegalStateException - 如果此节点不允许有子节点
      参见:
    • isNodeAncestor

      public boolean isNodeAncestor(TreeNode anotherNode)
      如果anotherNode是此节点的祖先(即为此节点、此节点的父节点或此节点父节点的祖先),则返回true。如果anotherNode为null,则此方法返回false。此操作的最坏情况时间复杂度为O(h),其中h为从根节点到此节点的距离。
      参数:
      anotherNode - 要测试是否为此节点祖先的节点
      返回:
      如果此节点是anotherNode的后代则返回true
      参见:
    • isNodeDescendant

      public boolean isNodeDescendant(DefaultMutableTreeNode anotherNode)
      如果anotherNode是此节点的后代(即为此节点、此节点的一个子节点或此节点的一个子节点的后代),则返回true。注意,节点被认为是其自身的后代。如果anotherNode为null,则返回false。此操作的最坏情况时间复杂度为O(h),其中h为从根节点到anotherNode的距离。
      Parameters:
      anotherNode - node to test as descendant of this node
      Returns:
      true if this node is an ancestor of anotherNode
      See Also:
    • getSharedAncestor

      public TreeNode getSharedAncestor(DefaultMutableTreeNode aNode)
      Returns the nearest common ancestor to this node and aNode. Returns null, if no such ancestor exists -- if this node and aNode are in different trees or if aNode is null. A node is considered an ancestor of itself.
      Parameters:
      aNode - node to find common ancestor with
      Returns:
      nearest ancestor common to this node and aNode, or null if none
      See Also:
    • isNodeRelated

      public boolean isNodeRelated(DefaultMutableTreeNode aNode)
      Returns true if and only if aNode is in the same tree as this node. Returns false if aNode is null.
      Parameters:
      aNode - node to find common ancestor with
      Returns:
      true if aNode is in the same tree as this node; false if aNode is null
      See Also:
    • getDepth

      public int getDepth()
      Returns the depth of the tree rooted at this node -- the longest distance from this node to a leaf. If this node has no children, returns 0. This operation is much more expensive than getLevel() because it must effectively traverse the entire tree rooted at this node.
      Returns:
      the depth of the tree whose root is this node
      See Also:
    • getLevel

      public int getLevel()
      Returns the number of levels above this node -- the distance from the root to this node. If this node is the root, returns 0.
      Returns:
      the number of levels above this node
      See Also:
    • getPath

      public TreeNode[] getPath()
      Returns the path from the root, to get to this node. The last element in the path is this node.
      Returns:
      an array of TreeNode objects giving the path, where the first element in the path is the root and the last element is this node.
    • getPathToRoot

      protected TreeNode[] getPathToRoot(TreeNode aNode, int depth)
      Builds the parents of node up to and including the root node, where the original node is the last element in the returned array. The length of the returned array gives the node's depth in the tree.
      Parameters:
      aNode - the TreeNode to get the path for
      depth - an int giving the number of steps already taken towards the root (on recursive calls), used to size the returned array
      Returns:
      an array of TreeNodes giving the path from the root to the specified node
    • getUserObjectPath

      public Object[] getUserObjectPath()
      Returns the user object path, from the root, to get to this node. If some of the TreeNodes in the path have null user objects, the returned path will contain nulls.
      Returns:
      the user object path, from the root, to get to this node
    • getRoot

      public TreeNode getRoot()
      Returns the root of the tree that contains this node. The root is the ancestor with a null parent.
      Returns:
      the root of the tree that contains this node
      See Also:
    • isRoot

      public boolean isRoot()
      Returns true if this node is the root of the tree. The root is the only node in the tree with a null parent; every tree has exactly one root.
      Returns:
      true if this node is the root of its tree
    • getNextNode

      public DefaultMutableTreeNode getNextNode()
      Returns the node that follows this node in a preorder traversal of this node's tree. Returns null if this node is the last node of the traversal. This is an inefficient way to traverse the entire tree; use an enumeration, instead.
      Returns:
      the node that follows this node in a preorder traversal, or null if this node is last
      See Also:
    • getPreviousNode

      public DefaultMutableTreeNode getPreviousNode()
      Returns the node that precedes this node in a preorder traversal of this node's tree. Returns null if this node is the first node of the traversal -- the root of the tree. This is an inefficient way to traverse the entire tree; use an enumeration, instead.
      Returns:
      the node that precedes this node in a preorder traversal, or null if this node is the first
      See Also:
    • preorderEnumeration

      public Enumeration<TreeNode> preorderEnumeration()
      Creates and returns an enumeration that traverses the subtree rooted at this node in preorder. The first node returned by the enumeration's nextElement() method is this node.

      Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.

      Returns:
      an enumeration for traversing the tree in preorder
      See Also:
    • postorderEnumeration

      public Enumeration<TreeNode> postorderEnumeration()
      Creates and returns an enumeration that traverses the subtree rooted at this node in postorder. The first node returned by the enumeration's nextElement() method is the leftmost leaf. This is the same as a depth-first traversal.

      Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.

      Returns:
      an enumeration for traversing the tree in postorder
      See Also:
    • breadthFirstEnumeration

      public Enumeration<TreeNode> breadthFirstEnumeration()
      Creates and returns an enumeration that traverses the subtree rooted at this node in breadth-first order. The first node returned by the enumeration's nextElement() method is this node.

      Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.

      Returns:
      an enumeration for traversing the tree in breadth-first order
      See Also:
    • depthFirstEnumeration

      public Enumeration<TreeNode> depthFirstEnumeration()
      Creates and returns an enumeration that traverses the subtree rooted at this node in depth-first order. The first node returned by the enumeration's nextElement() method is the leftmost leaf. This is the same as a postorder traversal.

      Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.

      Returns:
      an enumeration for traversing the tree in depth-first order
      See Also:
    • pathFromAncestorEnumeration

      public Enumeration<TreeNode> pathFromAncestorEnumeration(TreeNode ancestor)
      Creates and returns an enumeration that follows the path from ancestor to this node. The enumeration's nextElement() method first returns ancestor, then the child of ancestor that is an ancestor of this node, and so on, and finally returns this node. Creation of the enumeration is O(m) where m is the number of nodes between this node and ancestor, inclusive. Each nextElement() message is O(1).

      Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.

      Parameters:
      ancestor - the node to start enumeration from
      Returns:
      an enumeration for following the path from an ancestor of this node to this one
      Throws:
      IllegalArgumentException - if ancestor is not an ancestor of this node
      See Also:
    • isNodeChild

      public boolean isNodeChild(TreeNode aNode)
      Returns true if aNode is a child of this node. If aNode is null, this method returns false.
      Parameters:
      aNode - the node to determinate whether it is a child
      Returns:
      true if aNode is a child of this node; false if aNode is null
    • getFirstChild

      public TreeNode getFirstChild()
      Returns this node's first child. If this node has no children, throws NoSuchElementException.
      Returns:
      the first child of this node
      Throws:
      NoSuchElementException - if this node has no children
    • getLastChild

      public TreeNode getLastChild()
      Returns this node's last child. If this node has no children, throws NoSuchElementException.
      Returns:
      the last child of this node
      Throws:
      NoSuchElementException - if this node has no children
    • getChildAfter

      public TreeNode getChildAfter(TreeNode aChild)
      返回此节点的子节点数组中紧随 之后的子节点, aChild必须是此节点的子节点。如果 aChild是最后一个子节点,则返回null。此方法对此节点的子节点执行线性搜索以查找 aChild,时间复杂度为O(n),其中n是子节点的数量;要遍历所有子节点的数组,请改用枚举。
      参数:
      aChild - 要查找其后的下一个子节点的子节点
      返回:
      此节点中紧随aChild之后的子节点
      抛出:
      IllegalArgumentException - 如果aChild为null或不是此节点的子节点
      另请参见:
    • getChildBefore

      public TreeNode getChildBefore(TreeNode aChild)
      返回此节点的子节点数组中紧随aChild之前的子节点,aChild必须是此节点的子节点。如果aChild是第一个子节点,则返回null。此方法对此节点的子节点执行线性搜索以查找aChild,时间复杂度为O(n),其中n是子节点的数量。
      参数:
      aChild - 要查找其前的上一个子节点的子节点
      返回:
      此节点中紧随aChild之前的子节点
      抛出:
      IllegalArgumentException - 如果aChild为null或不是此节点的子节点
    • isNodeSibling

      public boolean isNodeSibling(TreeNode anotherNode)
      如果anotherNode是此节点的兄弟节点(具有相同的父节点),则返回true。节点本身是其自身的兄弟节点。如果anotherNode为null,则返回false。
      参数:
      anotherNode - 要测试是否为此节点的兄弟节点的节点
      返回:
      如果anotherNode是此节点的兄弟节点,则返回true
    • getSiblingCount

      public int getSiblingCount()
      返回此节点的兄弟节点数量。节点本身是其自身的兄弟节点(如果没有父节点或没有兄弟节点,则此方法返回1)。
      返回:
      此节点的兄弟节点数量
    • getNextSibling

      public DefaultMutableTreeNode getNextSibling()
      返回此节点在父节点的子节点数组中的下一个兄弟节点。如果此节点没有父节点或是父节点的最后一个子节点,则返回null。此方法执行线性搜索,时间复杂度为O(n),其中n是子节点的数量;要遍历整个数组,请改用父节点的子节点枚举。
      返回:
      立即跟随此节点的兄弟节点
      另请参见:
    • getPreviousSibling

      public DefaultMutableTreeNode getPreviousSibling()
      返回此节点在父节点的子节点数组中的上一个兄弟节点。如果此节点没有父节点或是父节点的第一个子节点,则返回null。此方法执行线性搜索,时间复杂度为O(n),其中n是子节点的数量。
      返回:
      立即在此节点之前的兄弟节点
    • isLeaf

      public boolean isLeaf()
      如果此节点没有子节点,则返回true。要区分没有子节点的节点和不能有子节点的节点(例如,区分文件和空目录),请结合使用此方法和getAllowsChildren
      指定者:
      isLeaf 在接口 TreeNode
      返回:
      如果此节点没有子节点,则返回true
      另请参见:
    • getFirstLeaf

      public DefaultMutableTreeNode getFirstLeaf()
      查找并返回作为此节点后代的第一个叶子节点 - 可能是此节点或其第一个子节点的第一个叶子节点。如果它是叶子节点,则返回此节点。
      返回:
      以此节点为根的子树中的第一个叶子节点
      另请参见:
    • getLastLeaf

      public DefaultMutableTreeNode getLastLeaf()
      查找并返回作为此节点后代的最后一个叶子节点 - 可能是此节点或其最后一个子节点的最后一个叶子节点。如果它是叶子节点,则返回此节点。
      返回:
      以此节点为根的子树中的最后一个叶子节点
      另请参见:
    • getNextLeaf

      public DefaultMutableTreeNode getNextLeaf()
      返回此节点之后的叶子节点,如果此节点是树中的最后一个叶子节点,则返回null。

      在此MutableNode接口的实现中,此操作效率非常低。为了确定下一个节点,此方法首先在父节点的子节点列表中执行线性搜索以找到当前节点。

      该实现使得该操作适用于从已知位置进行短暂遍历。但要遍历树中的所有叶子节点,应使用depthFirstEnumeration来枚举树中的节点,并对每个节点使用isLeaf来确定哪些是叶子节点。

      返回:
      返回此节点之后的下一个叶子节点
      另请参见:
    • getPreviousLeaf

      public DefaultMutableTreeNode getPreviousLeaf()
      返回此节点之前的叶子节点,如果此节点是树中的第一个叶子节点,则返回null。

      在此MutableNode接口的实现中,此操作效率非常低。为了确定前一个节点,此方法首先在父节点的子节点列表中执行线性搜索以找到当前节点。

      该实现使得该操作适用于从已知位置进行短暂遍历。但要遍历树中的所有叶子节点,应使用depthFirstEnumeration来枚举树中的节点,并对每个节点使用isLeaf来确定哪些是叶子节点。

      返回:
      返回此节点之前的叶子节点
      另请参见:
    • getLeafCount

      public int getLeafCount()
      返回作为此节点后代的叶子节点总数。如果此节点是叶子节点,则返回1。此方法的时间复杂度为O(n),其中n是此节点的后代数量。
      返回:
      此节点下的叶子节点数量
      另请参见:
    • toString

      public String toString()
      返回将toString()发送到此节点的用户对象的结果,如果节点没有用户对象,则返回空字符串。
      覆盖:
      toString 在类 Object
      返回:
      对象的字符串表示形式。
      另请参见:
    • clone

      public Object clone()
      覆盖以使克隆公开。返回此节点的浅拷贝;新节点没有父节点或子节点,并且如果有的话,具有对相同用户对象的引用。
      覆盖:
      clone 在类 Object
      返回:
      此节点的副本
      另请参见: