JTree.DynamicUtilTreeNode (Java SE 26 & JDK 26)
Description: declaration: module: java.desktop, package: javax.swing, class: JTree, class: DynamicUtilTreeNode
Keywords: javax.swing.JTree.DynamicUtilTreeNode class

# Class JTree.DynamicUtilTreeNode

java.lang.Object
javax.swing.tree.DefaultMutableTreeNode
javax.swing.JTree.DynamicUtilTreeNode

All Implemented Interfaces:
  `Serializable, Cloneable, MutableTreeNode, TreeNode`

Enclosing class:
  `JTree`

public static class JTree.DynamicUtilTreeNode extends DefaultMutableTreeNode
`DynamicUtilTreeNode` can wrap vectors/hashtables/arrays/strings and create the appropriate children tree nodes as necessary. It is dynamic in that it will only create the children as necessary.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the `java.beans` package. Please see `XMLEncoder`.

Since:
  1.2

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected Object`
  `childValue`
  Value to create children with.
  `protected boolean`
  `hasChildren`
  Does the this `JTree` have children?
  `protected boolean`
  `loadedChildren`
  Have the children been loaded yet?

  ### Fields declared in class DefaultMutableTreeNode

  `allowsChildren, children, EMPTY_ENUMERATION, parent, userObject`
  Modifier and Type
  Field
  Description
  `protected boolean`
  `allowsChildren`
  true if the node is able to have children
  `protected Vector<TreeNode>`
  `children`
  array of children, may be null if this node has no children
  `static final Enumeration<TreeNode>`
  `EMPTY_ENUMERATION`
  An enumeration that is always empty.
  `protected MutableTreeNode`
  `parent`
  this node's parent, or null if this node has no parent
  `protected Object`
  `userObject`
  optional user object

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `DynamicUtilTreeNode(Object value, Object children)`
  Creates a node with the specified object as its value and with the specified children.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Enumeration<TreeNode>`
  `children()`
  Subclassed to load the children, if necessary.
  `static void`
  `createChildren(DefaultMutableTreeNode parent, Object children)`
  Adds to parent all the children in `children`.
  `TreeNode`
  `getChildAt(int index)`
  Subclassed to load the children, if necessary.
  `int`
  `getChildCount()`
  Returns the number of child nodes.
  `boolean`
  `isLeaf()`
  Returns true if this node allows children.
  `protected void`
  `loadChildren()`
  Loads the children based on `childValue`.

  ### Methods declared in class DefaultMutableTreeNode

  `add, breadthFirstEnumeration, clone, depthFirstEnumeration, getAllowsChildren, getChildAfter, getChildBefore, getDepth, getFirstChild, getFirstLeaf, getIndex, getLastChild, getLastLeaf, getLeafCount, getLevel, getNextLeaf, getNextNode, getNextSibling, getParent, getPath, getPathToRoot, getPreviousLeaf, getPreviousNode, getPreviousSibling, getRoot, getSharedAncestor, getSiblingCount, getUserObject, getUserObjectPath, insert, isNodeAncestor, isNodeChild, isNodeDescendant, isNodeRelated, isNodeSibling, isRoot, pathFromAncestorEnumeration, postorderEnumeration, preorderEnumeration, remove, remove, removeAllChildren, removeFromParent, setAllowsChildren, setParent, setUserObject, toString`
  Modifier and Type
  Method
  Description
  `void`
  `add(MutableTreeNode newChild)`
  Removes `newChild` from its parent and makes it a child of this node by adding it to the end of this node's child array.
  `Enumeration<TreeNode>`
  `breadthFirstEnumeration()`
  Creates and returns an enumeration that traverses the subtree rooted at this node in breadth-first order.
  `Object`
  `clone()`
  Overridden to make clone public.
  `Enumeration<TreeNode>`
  `depthFirstEnumeration()`
  Creates and returns an enumeration that traverses the subtree rooted at this node in depth-first order.
  `boolean`
  `getAllowsChildren()`
  Returns true if this node is allowed to have children.
  `TreeNode`
  `getChildAfter(TreeNode aChild)`
  Returns the child in this node's child array that immediately follows `aChild`, which must be a child of this node.
  `TreeNode`
  `getChildBefore(TreeNode aChild)`
  Returns the child in this node's child array that immediately precedes `aChild`, which must be a child of this node.
  `int`
  `getDepth()`
  Returns the depth of the tree rooted at this node -- the longest distance from this node to a leaf.
  `TreeNode`
  `getFirstChild()`
  Returns this node's first child.
  `DefaultMutableTreeNode`
  `getFirstLeaf()`
  Finds and returns the first leaf that is a descendant of this node -- either this node or its first child's first leaf.
  `int`
  `getIndex(TreeNode aChild)`
  Returns the index of the specified child in this node's child array.
  `TreeNode`
  `getLastChild()`
  Returns this node's last child.
  `DefaultMutableTreeNode`
  `getLastLeaf()`
  Finds and returns the last leaf that is a descendant of this node -- either this node or its last child's last leaf.
  `int`
  `getLeafCount()`
  Returns the total number of leaves that are descendants of this node.
  `int`
  `getLevel()`
  Returns the number of levels above this node -- the distance from the root to this node.
  `DefaultMutableTreeNode`
  `getNextLeaf()`
  Returns the leaf after this node or null if this node is the last leaf in the tree.
  `DefaultMutableTreeNode`
  `getNextNode()`
  Returns the node that follows this node in a preorder traversal of this node's tree.
  `DefaultMutableTreeNode`
  `getNextSibling()`
  Returns the next sibling of this node in the parent's children array.
  `TreeNode`
  `getParent()`
  Returns this node's parent or null if this node has no parent.
  `TreeNode[]`
  `getPath()`
  Returns the path from the root, to get to this node.
  `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.
  `DefaultMutableTreeNode`
  `getPreviousLeaf()`
  Returns the leaf before this node or null if this node is the first leaf in the tree.
  `DefaultMutableTreeNode`
  `getPreviousNode()`
  Returns the node that precedes this node in a preorder traversal of this node's tree.
  `DefaultMutableTreeNode`
  `getPreviousSibling()`
  Returns the previous sibling of this node in the parent's children array.
  `TreeNode`
  `getRoot()`
  Returns the root of the tree that contains this node.
  `TreeNode`
  `getSharedAncestor(DefaultMutableTreeNode aNode)`
  Returns the nearest common ancestor to this node and `aNode`.
  `int`
  `getSiblingCount()`
  Returns the number of siblings of this node.
  `Object`
  `getUserObject()`
  Returns this node's user object.
  `Object[]`
  `getUserObjectPath()`
  Returns the user object path, from the root, to get to this node.
  `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`.
  `boolean`
  `isNodeAncestor(TreeNode anotherNode)`
  Returns true if `anotherNode` is an ancestor of this node -- if it is this node, this node's parent, or an ancestor of this node's parent.
  `boolean`
  `isNodeChild(TreeNode aNode)`
  Returns true if `aNode` is a child of this node.
  `boolean`
  `isNodeDescendant(DefaultMutableTreeNode anotherNode)`
  Returns true if `anotherNode` is a descendant of this node -- if it is this node, one of this node's children, or a descendant of one of this node's children.
  `boolean`
  `isNodeRelated(DefaultMutableTreeNode aNode)`
  Returns true if and only if `aNode` is in the same tree as this node.
  `boolean`
  `isNodeSibling(TreeNode anotherNode)`
  Returns true if `anotherNode` is a sibling of (has the same parent as) this node.
  `boolean`
  `isRoot()`
  Returns true if this node is the root of the tree.
  `Enumeration<TreeNode>`
  `pathFromAncestorEnumeration(TreeNode ancestor)`
  Creates and returns an enumeration that follows the path from `ancestor` to this node.
  `Enumeration<TreeNode>`
  `postorderEnumeration()`
  Creates and returns an enumeration that traverses the subtree rooted at this node in postorder.
  `Enumeration<TreeNode>`
  `preorderEnumeration()`
  Creates and returns an enumeration that traverses the subtree rooted at this node in preorder.
  `void`
  `remove(int childIndex)`
  Removes the child at the specified index from this node's children and sets that node's parent to null.
  `void`
  `remove(MutableTreeNode aChild)`
  Removes `aChild` from this node's child array, giving it a null parent.
  `void`
  `removeAllChildren()`
  Removes all of this node's children, setting their parents to null.
  `void`
  `removeFromParent()`
  Removes the subtree rooted at this node from the tree, giving this node a null parent.
  `void`
  `setAllowsChildren(boolean allows)`
  Determines whether or not this node is allowed to have children.
  `void`
  `setParent(MutableTreeNode newParent)`
  Sets this node's parent to `newParent` but does not change the parent's child array.
  `void`
  `setUserObject(Object userObject)`
  Sets the user object for this node to `userObject`.
  `String`
  `toString()`
  Returns the result of sending `toString()` to this node's user object, or the empty string if the node has no user object.

  ### Methods declared in class Object

  `equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Field Details

  - ### hasChildren

    protected boolean hasChildren
    Does the this `JTree` have children? This property is currently not implemented.

  - ### childValue

    protected Object childValue
    Value to create children with.

  - ### loadedChildren

    protected boolean loadedChildren
    Have the children been loaded yet?

- ## Constructor Details

  - ### DynamicUtilTreeNode

    public DynamicUtilTreeNode(Object value, Object children)
    Creates a node with the specified object as its value and with the specified children. For the node to allow children, the children-object must be an array of objects, a `Vector`, or a `Hashtable` -- even if empty. Otherwise, the node is not allowed to have children.
    Parameters:
      `value` - the `Object` that is the value for the new node
      `children` - an array of `Object`s, a `Vector`, or a `Hashtable` used to create the child nodes; if any other object is specified, or if the value is `null`, then the node is not allowed to have children

- ## Method Details

  - ### createChildren

    public static void createChildren(DefaultMutableTreeNode parent, Object children)
    Adds to parent all the children in `children`. If `children` is an array or vector all of its elements are added is children, otherwise if `children` is a hashtable all the key/value pairs are added in the order `Enumeration` returns them.
    Parameters:
      `parent` - the parent node
      `children` - the children

  - ### isLeaf

    public boolean isLeaf()
    Returns true if this node allows children. Whether the node allows children depends on how it was created.
    Specified by:
      `isLeaf` in interface `TreeNode`
    Overrides:
      `isLeaf` in class `DefaultMutableTreeNode`
    Returns:
      true if this node allows children, false otherwise
    See Also:
      - `JTree.DynamicUtilTreeNode`

  - ### getChildCount

    public int getChildCount()
    Returns the number of child nodes.
    Specified by:
      `getChildCount` in interface `TreeNode`
    Overrides:
      `getChildCount` in class `DefaultMutableTreeNode`
    Returns:
      the number of child nodes

  - ### loadChildren

    protected void loadChildren()
    Loads the children based on `childValue`. If `childValue` is a `Vector` or array each element is added as a child, if `childValue` is a `Hashtable` each key/value pair is added in the order that `Enumeration` returns the keys.

  - ### getChildAt

    public TreeNode getChildAt(int index)
    Subclassed to load the children, if necessary.
    Specified by:
      `getChildAt` in interface `TreeNode`
    Overrides:
      `getChildAt` in class `DefaultMutableTreeNode`
    Parameters:
      `index` - an index into this node's child array
    Returns:
      the TreeNode in this node's child array at the specified index

  - ### children

    public Enumeration<TreeNode> children()
    Subclassed to load the children, if necessary.
    Specified by:
      `children` in interface `TreeNode`
    Overrides:
      `children` in class `DefaultMutableTreeNode`
    Returns:
      an Enumeration of this node's children