APIdia Launch Partner
Top Description Fields Constructors Methods
javax.swing.tree

public Class DefaultTreeModel

extends Object
implements Serializable, TreeModel
Class Inheritance
All Implemented Interfaces
javax.swing.tree.TreeModel, java.io.Serializable
Annotations
@SuppressWarnings:serial
Imports
java.beans.ConstructorProperties, java.io.IOException, .InvalidObjectException, .ObjectInputStream, .ObjectOutputStream, .Serial, .Serializable, java.util.EventListener, .Vector, javax.swing.event.EventListenerList, .TreeModelEvent, .TreeModelListener

A simple tree data model that uses TreeNodes. For further information and examples that use DefaultTreeModel, see How to Use Trees in The Java Tutorial.

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 java.beans.XMLEncoder.

Authors
Rob Davis, Ray Ryan, Scott Violet

Field Summary

Modifier and TypeField and 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 Summary

AccessConstructor and Description
public
DefaultTreeModel(TreeNode
a TreeNode object that is the root of the tree
root
)

Creates a tree in which any node can have children.

public
DefaultTreeModel(TreeNode
a TreeNode object that is the root of the tree
root
,
boolean
a boolean, false if any node can have children, true if each node is asked to see if it can have children
asksAllowsChildren
)

Creates a tree specifying whether any node can have children, or whether only certain nodes can have children.

Method Summary

Modifier and TypeMethod and Description
public void
addTreeModelListener(TreeModelListener
the listener to add
l
)

Implements javax.swing.tree.TreeModel.addTreeModelListener.

Adds a listener for the TreeModelEvent posted after the tree changes.

public boolean

Returns:

true if only nodes which do not allow children are leaf nodes, false if nodes which have no children (even if allowed) are leaf nodes
asksAllowsChildren
()

Tells how leaf nodes are determined.

protected void
fireTreeNodesChanged(Object
the source of the TreeModelEvent; typically this
source
,
Object[]
the path to the parent of the nodes that changed; use null to identify the root has changed
path
,
int[]
the indices of the changed elements
childIndices
,
Object[]
the changed elements
children
)

Notifies all listeners that have registered interest for notification on this event type.

protected void
fireTreeNodesInserted(Object
the source of the TreeModelEvent; typically this
source
,
Object[]
the path to the parent the nodes were added to
path
,
int[]
the indices of the new elements
childIndices
,
Object[]
the new elements
children
)

Notifies all listeners that have registered interest for notification on this event type.

protected void
fireTreeNodesRemoved(Object
the source of the TreeModelEvent; typically this
source
,
Object[]
the path to the parent the nodes were removed from
path
,
int[]
the indices of the removed elements
childIndices
,
Object[]
the removed elements
children
)

Notifies all listeners that have registered interest for notification on this event type.

protected void
fireTreeStructureChanged(Object
the source of the TreeModelEvent; typically this
source
,
Object[]
the path to the parent of the structure that has changed; use null to identify the root has changed
path
,
int[]
the indices of the affected elements
childIndices
,
Object[]
the affected elements
children
)

Notifies all listeners that have registered interest for notification on this event type.

private void
fireTreeStructureChanged(Object
the source of the TreeModelEvent; typically this
source
,
TreePath
the path to the parent of the structure that has changed; use null to identify the root has changed
path
)

Notifies all listeners that have registered interest for notification on this event type.

public Object

Returns:

the child of parent at index index
getChild
(Object
a node in the tree, obtained from this data source
parent
,
int index)

Implements javax.swing.tree.TreeModel.getChild.

Returns the child of parent at index index in the parent's child array.

public int

Returns:

the number of children of the node parent
getChildCount
(Object
a node in the tree, obtained from this data source
parent
)

Implements javax.swing.tree.TreeModel.getChildCount.

Returns the number of children of parent.

public int

Returns:

the index of the child in the parent, or -1 if either the parent or the child is null
getIndexOfChild
(Object
a note in the tree, obtained from this data source
parent
,
Object
the node we are interested in
child
)

Implements javax.swing.tree.TreeModel.getIndexOfChild.

Returns the index of child in parent.

public <
the listener type
T extends EventListener
>
T[]

Returns:

an array of all objects registered as FooListeners on this component, or an empty array if no such listeners have been added
getListeners
(Class<T>
the type of listeners requested
listenerType
)

Returns an array of all the objects currently registered as FooListeners upon this model.

public TreeNode[]

Returns:

an array of TreeNodes giving the path from the root
getPathToRoot
(TreeNode
the TreeNode to get the path for
aNode
)

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[]

Returns:

an array of TreeNodes giving the path from the root to the specified node
getPathToRoot
(TreeNode
the TreeNode to get the path for
aNode
,
int
an int giving the number of steps already taken towards the root (on recursive calls), used to size the returned array
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.

public Object

Returns:

the root of the tree
getRoot
()

Implements javax.swing.tree.TreeModel.getRoot.

Returns the root of the tree.

public TreeModelListener[]

Returns:

all of this model's TreeModelListeners or an empty array if no tree model listeners are currently registered
getTreeModelListeners
()

Returns an array of all the tree model listeners registered on this model.

public void
insertNodeInto(MutableTreeNode
child node to be inserted
newChild
,
MutableTreeNode
node to which children new node will be added
parent
,
int
index of parent's children
index
)

Invoked this to insert newChild at location index in parents children.

public boolean

Returns:

true if the node is a leaf node
isLeaf
(Object
the node to check
node
)

Implements javax.swing.tree.TreeModel.isLeaf.

Returns whether the specified node is a leaf node.

public void
nodeChanged(TreeNode
the changed node
node
)

Invoke this method after you've changed how node is to be represented in the tree.

public void
nodesChanged(TreeNode
changed node
node
,
int[]
indexes of changed children
childIndices
)

Invoke this method after you've changed how the children identified by childIndices are to be represented in the tree.

public void
nodeStructureChanged(TreeNode
changed node
node
)

Invoke this method if you've totally changed the children of node and its children's children...

public void
nodesWereInserted(TreeNode
parent node which children count been incremented
node
,
int[]
indexes of inserted children
childIndices
)

Invoke this method after you've inserted some TreeNodes into node.

public void
nodesWereRemoved(TreeNode
parent node from which children were removed
node
,
int[]
indexes of removed children
childIndices
,
Object[]
array of the children objects that were removed
removedChildren
)

Invoke this method after you've removed some TreeNodes from node.

private void
public void
reload()

Invoke this method if you've modified the TreeNodes upon which this model depends.

public void
reload(TreeNode
the node below which the model has changed
node
)

Invoke this method if you've modified the TreeNodes upon which this model depends.

public void
removeNodeFromParent(MutableTreeNode
the node to be removed from its parent
node
)

Message this to remove node from its parent.

public void
removeTreeModelListener(TreeModelListener
the listener to remove
l
)

Implements javax.swing.tree.TreeModel.removeTreeModelListener.

Removes a listener previously added with addTreeModelListener().

public void
setAsksAllowsChildren(boolean
if true, getAllowsChildren() is messaged, otherwise isLeaf() is messaged
newValue
)

Sets whether or not to test leafness by asking getAllowsChildren() or isLeaf() to the TreeNodes.

public void
setRoot(TreeNode
new value of tree root
root
)

Sets the root to root.

public void
valueForPathChanged(TreePath path, Object newValue)

Implements javax.swing.tree.TreeModel.valueForPathChanged.

This sets the user object of the TreeNode identified by path and posts a node changed.

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

asksAllowsChildrenback to summary
protected boolean asksAllowsChildren

Determines how the 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.

See Also
TreeNode#getAllowsChildren, TreeModel#isLeaf, setAsksAllowsChildren
listenerListback to summary
protected EventListenerList listenerList

Listeners.

rootback to summary
protected TreeNode root

Root of the tree.

Constructor Detail

DefaultTreeModelback to summary
public DefaultTreeModel(TreeNode root)

Creates a tree in which any node can have children.

Parameters
root:TreeNode

a TreeNode object that is the root of the tree

Annotations
@ConstructorProperties:root
See Also
DefaultTreeModel(TreeNode, boolean)
DefaultTreeModelback to summary
public DefaultTreeModel(TreeNode root, boolean asksAllowsChildren)

Creates a tree specifying whether any node can have children, or whether only certain nodes can have children.

Parameters
root:TreeNode

a TreeNode object that is the root of the tree

asksAllowsChildren:boolean

a boolean, false if any node can have children, true if each node is asked to see if it can have children

See Also
asksAllowsChildren

Method Detail

addTreeModelListenerback to summary
public void addTreeModelListener(TreeModelListener l)

Implements javax.swing.tree.TreeModel.addTreeModelListener.

Adds a listener for the TreeModelEvent posted after the tree changes.

Parameters
l:TreeModelListener

the listener to add

See Also
removeTreeModelListener
asksAllowsChildrenback to summary
public boolean asksAllowsChildren()

Tells how leaf nodes are determined.

Returns:boolean

true if only nodes which do not allow children are leaf nodes, false if nodes which have no children (even if allowed) are leaf nodes

See Also
asksAllowsChildren
fireTreeNodesChangedback to summary
protected void fireTreeNodesChanged(Object source, Object[] path, int[] childIndices, Object[] children)

Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Parameters
source:Object

the source of the TreeModelEvent; typically this

path:Object[]

the path to the parent of the nodes that changed; use null to identify the root has changed

childIndices:int[]

the indices of the changed elements

children:Object[]

the changed elements

fireTreeNodesInsertedback to summary
protected void fireTreeNodesInserted(Object source, Object[] path, int[] childIndices, Object[] children)

Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Parameters
source:Object

the source of the TreeModelEvent; typically this

path:Object[]

the path to the parent the nodes were added to

childIndices:int[]

the indices of the new elements

children:Object[]

the new elements

fireTreeNodesRemovedback to summary
protected void fireTreeNodesRemoved(Object source, Object[] path, int[] childIndices, Object[] children)

Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Parameters
source:Object

the source of the TreeModelEvent; typically this

path:Object[]

the path to the parent the nodes were removed from

childIndices:int[]

the indices of the removed elements

children:Object[]

the removed elements

fireTreeStructureChangedback to summary
protected void fireTreeStructureChanged(Object source, Object[] path, int[] childIndices, Object[] children)

Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Parameters
source:Object

the source of the TreeModelEvent; typically this

path:Object[]

the path to the parent of the structure that has changed; use null to identify the root has changed

childIndices:int[]

the indices of the affected elements

children:Object[]

the affected elements

fireTreeStructureChangedback to summary
private void fireTreeStructureChanged(Object source, TreePath path)

Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Parameters
source:Object

the source of the TreeModelEvent; typically this

path:TreePath

the path to the parent of the structure that has changed; use null to identify the root has changed

getChildback to summary
public Object getChild(Object parent, int index)

Implements javax.swing.tree.TreeModel.getChild.

Returns the child of parent at index index in the parent's child array. parent must be a node previously obtained from this data source. This should not return null if index is a valid index for parent (that is index >= 0 && index < getChildCount(parent)).

Parameters
parent:Object

a node in the tree, obtained from this data source

Returns:Object

the child of parent at index index

getChildCountback to summary
public int getChildCount(Object parent)

Implements javax.swing.tree.TreeModel.getChildCount.

Returns the number of children of parent. Returns 0 if the node is a leaf or if it has no children. parent must be a node previously obtained from this data source.

Parameters
parent:Object

a node in the tree, obtained from this data source

Returns:int

the number of children of the node parent

getIndexOfChildback to summary
public int getIndexOfChild(Object parent, Object child)

Implements javax.swing.tree.TreeModel.getIndexOfChild.

Returns the index of child in parent. If either the parent or child is null, returns -1.

Parameters
parent:Object

a note in the tree, obtained from this data source

child:Object

the node we are interested in

Returns:int

the index of the child in the parent, or -1 if either the parent or the child is null

getListenersback to summary
public <T extends EventListener> T[] getListeners(Class<T> listenerType)

Returns an array of all the objects currently registered as FooListeners upon this model. FooListeners 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.
Parameters
<T>
the listener type
listenerType:Class<T>

the type of listeners requested

Returns:T[]

an array of all objects registered as FooListeners on this component, or an empty array if no such listeners have been added

Exceptions
ClassCastException:
if listenerType doesn't specify a class or interface that implements java.util.EventListener
Since
1.3
See Also
getTreeModelListeners
getPathToRootback to summary
public TreeNode[] getPathToRoot(TreeNode aNode)

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:TreeNode

the TreeNode to get the path for

Returns:TreeNode[]

an array of TreeNodes giving the path from the root

getPathToRootback to summary
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:TreeNode

the TreeNode to get the path for

depth:int

an int giving the number of steps already taken towards the root (on recursive calls), used to size the returned array

Returns:TreeNode[]

an array of TreeNodes giving the path from the root to the specified node

getRootback to summary
public Object getRoot()

Implements javax.swing.tree.TreeModel.getRoot.

Returns the root of the tree. Returns null only if the tree has no nodes.

Returns:Object

the root of the tree

getTreeModelListenersback to summary
public TreeModelListener[] getTreeModelListeners()

Returns an array of all the tree model listeners registered on this model.

Returns:TreeModelListener[]

all of this model's TreeModelListeners or an empty array if no tree model listeners are currently registered

Since
1.4
See Also
addTreeModelListener, removeTreeModelListener
insertNodeIntoback to summary
public void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index)

Invoked this to insert newChild at location index in parents children. This will then message nodesWereInserted to create the appropriate event. This is the preferred way to add children as it will create the appropriate event.

Parameters
newChild:MutableTreeNode

child node to be inserted

parent:MutableTreeNode

node to which children new node will be added

index:int

index of parent's children

isLeafback to summary
public boolean isLeaf(Object node)

Implements javax.swing.tree.TreeModel.isLeaf.

Returns whether the specified node is a leaf node. The way the test is performed depends on the asksAllowsChildren setting.

Parameters
node:Object

the node to check

Returns:boolean

true if the node is a leaf node

See Also
asksAllowsChildren, TreeModel#isLeaf
nodeChangedback to summary
public void nodeChanged(TreeNode node)

Invoke this method after you've changed how node is to be represented in the tree.

Parameters
node:TreeNode

the changed node

nodesChangedback to summary
public void nodesChanged(TreeNode node, int[] childIndices)

Invoke this method after you've changed how the children identified by childIndices are to be represented in the tree.

Parameters
node:TreeNode

changed node

childIndices:int[]

indexes of changed children

nodeStructureChangedback to summary
public void nodeStructureChanged(TreeNode node)

Invoke this method if you've totally changed the children of node and its children's children... This will post a treeStructureChanged event.

Parameters
node:TreeNode

changed node

nodesWereInsertedback to summary
public void nodesWereInserted(TreeNode node, int[] childIndices)

Invoke this method after you've inserted some TreeNodes into node. childIndices should be the index of the new elements and must be sorted in ascending order.

Parameters
node:TreeNode

parent node which children count been incremented

childIndices:int[]

indexes of inserted children

nodesWereRemovedback to summary
public void nodesWereRemoved(TreeNode node, int[] childIndices, Object[] removedChildren)

Invoke this method after you've removed some TreeNodes from node. childIndices should be the index of the removed elements and must be sorted in ascending order. And removedChildren should be the array of the children objects that were removed.

Parameters
node:TreeNode

parent node from which children were removed

childIndices:int[]

indexes of removed children

removedChildren:Object[]

array of the children objects that were removed

readObjectback to summary
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
Annotations
@Serial
reloadback to summary
public void reload()

Invoke this method if you've modified the TreeNodes upon which this model depends. The model will notify all of its listeners that the model has changed.

reloadback to summary
public void reload(TreeNode node)

Invoke this method if you've modified the TreeNodes upon which this model depends. The model will notify all of its listeners that the model has changed below the given node.

Parameters
node:TreeNode

the node below which the model has changed

removeNodeFromParentback to summary
public void removeNodeFromParent(MutableTreeNode node)

Message this to remove node from its parent. This will message nodesWereRemoved to create the appropriate event. This is the preferred way to remove a node as it handles the event creation for you.

Parameters
node:MutableTreeNode

the node to be removed from its parent

removeTreeModelListenerback to summary
public void removeTreeModelListener(TreeModelListener l)

Implements javax.swing.tree.TreeModel.removeTreeModelListener.

Removes a listener previously added with addTreeModelListener().

Parameters
l:TreeModelListener

the listener to remove

See Also
addTreeModelListener
setAsksAllowsChildrenback to summary
public void setAsksAllowsChildren(boolean newValue)

Sets whether or not to test leafness by asking getAllowsChildren() or isLeaf() to the TreeNodes. If newvalue is true, getAllowsChildren() is messaged, otherwise isLeaf() is messaged.

Parameters
newValue:boolean

if true, getAllowsChildren() is messaged, otherwise isLeaf() is messaged

setRootback to summary
public void setRoot(TreeNode root)

Sets the root to root. A null root implies the tree is to display nothing, and is legal.

Parameters
root:TreeNode

new value of tree root

valueForPathChangedback to summary
public void valueForPathChanged(TreePath path, Object newValue)

Implements javax.swing.tree.TreeModel.valueForPathChanged.

This sets the user object of the TreeNode identified by path and posts a node changed. If you use custom user objects in the TreeModel you're going to need to subclass this and set the user object of the changed node to something meaningful.

writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException
Annotations
@Serial