Serializable
, TreeModel
public class DefaultTreeModel extends Object implements Serializable, TreeModel
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
.
Modifier and Type | Field | Description |
---|---|---|
protected boolean |
asksAllowsChildren |
Determines how the isLeaf method figures out if a node is a leaf node. |
protected EventListenerList |
listenerList |
Listeners. |
protected TreeNode |
root |
Root of the tree. |
Constructor | Description |
---|---|
DefaultTreeModel |
Creates a tree in which any node can have children. |
DefaultTreeModel |
Creates a tree specifying whether any node can have children, or whether only certain nodes can have children. |
Modifier and Type | Method | Description |
---|---|---|
void |
addTreeModelListener |
Adds a listener for the TreeModelEvent posted after the tree changes. |
boolean |
asksAllowsChildren() |
Tells how leaf nodes are determined. |
protected void |
fireTreeNodesChanged |
Notifies all listeners that have registered interest for notification on this event type. |
protected void |
fireTreeNodesInserted |
Notifies all listeners that have registered interest for notification on this event type. |
protected void |
fireTreeNodesRemoved |
Notifies all listeners that have registered interest for notification on this event type. |
protected void |
fireTreeStructureChanged |
Notifies all listeners that have registered interest for notification on this event type. |
Object |
getChild |
Returns the child of parent at index index in the parent's child array. |
int |
getChildCount |
Returns the number of children of parent. |
int |
getIndexOfChild |
Returns the index of child in parent. |
<T extends EventListener> |
getListeners |
Returns an array of all the objects currently registered as FooListener s upon this model. |
TreeNode[] |
getPathToRoot |
Builds the parents of node up to and including the root node, where the original node is the last element in the returned array. |
protected TreeNode[] |
getPathToRoot |
Builds the parents of node up to and including the root node, where the original node is the last element in the returned array. |
Object |
getRoot() |
Returns the root of the tree. |
TreeModelListener[] |
getTreeModelListeners() |
Returns an array of all the tree model listeners registered on this model. |
void |
insertNodeInto |
Invoked this to insert newChild at location index in parents children. |
boolean |
isLeaf |
Returns whether the specified node is a leaf node. |
void |
nodeChanged |
Invoke this method after you've changed how node is to be represented in the tree. |
void |
nodesChanged |
Invoke this method after you've changed how the children identified by childIndicies are to be represented in the tree. |
void |
nodeStructureChanged |
Invoke this method if you've totally changed the children of node and its children's children... |
void |
nodesWereInserted |
Invoke this method after you've inserted some TreeNodes into node. |
void |
nodesWereRemoved |
Invoke this method after you've removed some TreeNodes from node. |
void |
reload() |
Invoke this method if you've modified the TreeNode s upon which this model depends. |
void |
reload |
Invoke this method if you've modified the TreeNode s upon which this model depends. |
void |
removeNodeFromParent |
Message this to remove node from its parent. |
void |
removeTreeModelListener |
Removes a listener previously added with addTreeModelListener(). |
void |
setAsksAllowsChildren |
Sets whether or not to test leafness by asking getAllowsChildren() or isLeaf() to the TreeNodes. |
void |
setRoot |
Sets the root to root . |
void |
valueForPathChanged |
This sets the user object of the TreeNode identified by path and posts a node changed. |
protected TreeNode root
protected EventListenerList listenerList
protected boolean asksAllowsChildren
isLeaf
method figures out if a node is a leaf node. If true, a node is a leaf node if it does not allow children. (If it allows children, it is not a leaf node, even if no children are present.) That lets you distinguish between folder nodes and file nodes in a file system, for example. If this value is false, then any node which has no children is a leaf node, and any node may acquire children.
@ConstructorProperties("root") public DefaultTreeModel(TreeNode root)
root
- a TreeNode object that is the root of the treepublic DefaultTreeModel(TreeNode root, boolean asksAllowsChildren)
root
- a TreeNode object that is the root of the treeasksAllowsChildren
- a boolean, false if any node can have children, true if each node is asked to see if it can have childrenpublic void setAsksAllowsChildren(boolean newValue)
newValue
- if true, getAllowsChildren() is messaged, otherwise isLeaf() is messagedpublic boolean asksAllowsChildren()
public void setRoot(TreeNode root)
root
. A null root
implies the tree is to display nothing, and is legal.root
- new value of tree rootpublic Object getRoot()
public int getIndexOfChild(Object parent, Object child)
null
, returns -1.getIndexOfChild
in interface TreeModel
parent
- a note in the tree, obtained from this data sourcechild
- the node we are interested innull
public Object getChild(Object parent, int index)
public int getChildCount(Object parent)
getChildCount
in interface TreeModel
parent
- a node in the tree, obtained from this data sourcepublic boolean isLeaf(Object node)
asksAllowsChildren
setting.public void reload()
TreeNode
s upon which this model depends. The model will notify all of its listeners that the model has changed.public void valueForPathChanged(TreePath path, Object newValue)
valueForPathChanged
in interface TreeModel
path
- path to the node that the user has alterednewValue
- the new value from the TreeCellEditorpublic void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index)
newChild
- child node to be insertedparent
- node to which children new node will be addedindex
- index of parent's childrenpublic void removeNodeFromParent(MutableTreeNode node)
node
- the node to be removed from its parentpublic void nodeChanged(TreeNode node)
node
- the changed nodepublic void reload(TreeNode node)
TreeNode
s upon which this model depends. The model will notify all of its listeners that the model has changed below the given node.node
- the node below which the model has changedpublic void nodesWereInserted(TreeNode node, int[] childIndices)
node
- parent node which children count been incrementedchildIndices
- indexes of inserted childrenpublic void nodesWereRemoved(TreeNode node, int[] childIndices, Object[] removedChildren)
node
- parent node which childred were removedchildIndices
- indexes of removed childrenremovedChildren
- array of the children objects that were removedpublic void nodesChanged(TreeNode node, int[] childIndices)
node
- changed nodechildIndices
- indexes of changed childrenpublic void nodeStructureChanged(TreeNode node)
node
- changed nodepublic TreeNode[] getPathToRoot(TreeNode aNode)
aNode
- the TreeNode to get the path forprotected TreeNode[] getPathToRoot(TreeNode aNode, int depth)
aNode
- the TreeNode to get the path fordepth
- an int giving the number of steps already taken towards the root (on recursive calls), used to size the returned arraypublic void addTreeModelListener(TreeModelListener l)
addTreeModelListener
in interface TreeModel
l
- the listener to addpublic void removeTreeModelListener(TreeModelListener l)
removeTreeModelListener
in interface TreeModel
l
- the listener to removepublic TreeModelListener[] getTreeModelListeners()
TreeModelListener
s or an empty array if no tree model listeners are currently registeredprotected void fireTreeNodesChanged(Object source, Object[] path, int[] childIndices, Object[] children)
source
- the source of the TreeModelEvent
; typically this
path
- the path to the parent of the nodes that changed; use null
to identify the root has changedchildIndices
- the indices of the changed elementschildren
- the changed elementsprotected void fireTreeNodesInserted(Object source, Object[] path, int[] childIndices, Object[] children)
source
- the source of the TreeModelEvent
; typically this
path
- the path to the parent the nodes were added tochildIndices
- the indices of the new elementschildren
- the new elementsprotected void fireTreeNodesRemoved(Object source, Object[] path, int[] childIndices, Object[] children)
source
- the source of the TreeModelEvent
; typically this
path
- the path to the parent the nodes were removed fromchildIndices
- the indices of the removed elementschildren
- the removed elementsprotected void fireTreeStructureChanged(Object source, Object[] path, int[] childIndices, Object[] children)
source
- the source of the TreeModelEvent
; typically this
path
- the path to the parent of the structure that has changed; use null
to identify the root has changedchildIndices
- the indices of the affected elementschildren
- the affected elementspublic <T extends EventListener> T[] getListeners(Class<T> listenerType)
FooListener
s upon this model. FooListener
s are registered using the addFooListener
method. You can specify the listenerType
argument with a class literal, such as FooListener.class
. For example, you can query a DefaultTreeModel
m
for its tree model listeners with the following code:
TreeModelListener[] tmls = (TreeModelListener[])(m.getListeners(TreeModelListener.class));If no such listeners exist, this method returns an empty array.
T
- the listener typelistenerType
- the type of listeners requestedFooListener
s on this component, or an empty array if no such listeners have been addedClassCastException
- if listenerType
doesn't specify a class or interface that implements java.util.EventListener
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.desktop/javax/swing/tree/DefaultTreeModel.html