Class DefaultTreeModel
java.lang.Object
javax.swing.tree.DefaultTreeModel
- All Implemented Interfaces:
- Serializable,- TreeModel
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 XMLEncoder.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected booleanDetermines how theisLeafmethod figures out if a node is a leaf node.protected EventListenerListListeners.protected TreeNodeRoot of the tree.
- 
Constructor SummaryConstructorsConstructorDescriptionDefaultTreeModel(TreeNode root) Creates a tree in which any node can have children.DefaultTreeModel(TreeNode root, boolean asksAllowsChildren) Creates a tree specifying whether any node can have children, or whether only certain nodes can have children.
- 
Method SummaryModifier and TypeMethodDescriptionvoidAdds a listener for the TreeModelEvent posted after the tree changes.booleanTells how leaf nodes are determined.protected voidfireTreeNodesChanged(Object source, Object[] path, int[] childIndices, Object[] children) Notifies all listeners that have registered interest for notification on this event type.protected voidfireTreeNodesInserted(Object source, Object[] path, int[] childIndices, Object[] children) Notifies all listeners that have registered interest for notification on this event type.protected voidfireTreeNodesRemoved(Object source, Object[] path, int[] childIndices, Object[] children) Notifies all listeners that have registered interest for notification on this event type.protected voidfireTreeStructureChanged(Object source, Object[] path, int[] childIndices, Object[] children) Notifies all listeners that have registered interest for notification on this event type.Returns the child of parent at index index in the parent's child array.intgetChildCount(Object parent) Returns the number of children of parent.intgetIndexOfChild(Object parent, Object child) Returns the index of child in parent.<T extends EventListener>
 T[]getListeners(Class<T> listenerType) Returns an array of all the objects currently registered asFooListeners upon this model.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.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.getRoot()Returns the root of the tree.Returns an array of all the tree model listeners registered on this model.voidinsertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) Invoked this to insert newChild at location index in parents children.booleanReturns whether the specified node is a leaf node.voidnodeChanged(TreeNode node) Invoke this method after you've changed how node is to be represented in the tree.voidnodesChanged(TreeNode node, int[] childIndices) Invoke this method after you've changed how the children identified by childIndices are to be represented in the tree.voidnodeStructureChanged(TreeNode node) Invoke this method if you've totally changed the children of node and its children's children...voidnodesWereInserted(TreeNode node, int[] childIndices) Invoke this method after you've inserted some TreeNodes into node.voidnodesWereRemoved(TreeNode node, int[] childIndices, Object[] removedChildren) Invoke this method after you've removed some TreeNodes from node.voidreload()Invoke this method if you've modified theTreeNodes upon which this model depends.voidInvoke this method if you've modified theTreeNodes upon which this model depends.voidMessage this to remove node from its parent.voidRemoves a listener previously added with addTreeModelListener().voidsetAsksAllowsChildren(boolean newValue) Sets whether or not to test leafness by asking getAllowsChildren() or isLeaf() to the TreeNodes.voidSets the root toroot.voidvalueForPathChanged(TreePath path, Object newValue) This sets the user object of the TreeNode identified by path and posts a node changed.
- 
Field Details- 
rootRoot of the tree.
- 
listenerListListeners.
- 
asksAllowsChildrenprotected boolean asksAllowsChildrenDetermines how theisLeafmethod 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:
 
 
- 
- 
Constructor Details- 
DefaultTreeModelCreates a tree in which any node can have children.- Parameters:
- root- a TreeNode object that is the root of the tree
- See Also:
 
- 
DefaultTreeModelCreates a tree specifying whether any node can have children, or whether only certain nodes can have children.- Parameters:
- root- a TreeNode object that is the root of the tree
- asksAllowsChildren- a boolean, false if any node can have children, true if each node is asked to see if it can have children
- See Also:
 
 
- 
- 
Method Details- 
setAsksAllowsChildrenpublic 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- if true, getAllowsChildren() is messaged, otherwise isLeaf() is messaged
 
- 
asksAllowsChildrenpublic boolean asksAllowsChildren()Tells how leaf nodes are determined.- 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
- See Also:
 
- 
setRootSets the root toroot. A nullrootimplies the tree is to display nothing, and is legal.- Parameters:
- root- new value of tree root
 
- 
getRoot
- 
getIndexOfChildReturns the index of child in parent. If either the parent or child isnull, returns -1.- Specified by:
- getIndexOfChildin interface- TreeModel
- Parameters:
- parent- a note in the tree, obtained from this data source
- child- the node we are interested in
- Returns:
- the index of the child in the parent, or -1
    if either the parent or the child is null
 
- 
getChildReturns 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)).
- 
getChildCountReturns 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.- Specified by:
- getChildCountin interface- TreeModel
- Parameters:
- parent- a node in the tree, obtained from this data source
- Returns:
- the number of children of the node parent
 
- 
isLeaf
- 
reloadpublic void reload()Invoke this method if you've modified theTreeNodes upon which this model depends. The model will notify all of its listeners that the model has changed.
- 
valueForPathChangedThis 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.- Specified by:
- valueForPathChangedin interface- TreeModel
- Parameters:
- path- path to the node that the user has altered
- newValue- the new value from the TreeCellEditor
 
- 
insertNodeIntoInvoked 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- child node to be inserted
- parent- node to which children new node will be added
- index- index of parent's children
 
- 
removeNodeFromParentMessage 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- the node to be removed from its parent
 
- 
nodeChangedInvoke this method after you've changed how node is to be represented in the tree.- Parameters:
- node- the changed node
 
- 
reloadInvoke this method if you've modified theTreeNodes upon which this model depends. The model will notify all of its listeners that the model has changed below the given node.- Parameters:
- node- the node below which the model has changed
 
- 
nodesWereInsertedInvoke 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- parent node which children count been incremented
- childIndices- indexes of inserted children
 
- 
nodesWereRemovedInvoke 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- parent node from which children were removed
- childIndices- indexes of removed children
- removedChildren- array of the children objects that were removed
 
- 
nodesChangedInvoke this method after you've changed how the children identified by childIndices are to be represented in the tree.- Parameters:
- node- changed node
- childIndices- indexes of changed children
 
- 
nodeStructureChangedInvoke this method if you've totally changed the children of node and its children's children... This will post a treeStructureChanged event.- Parameters:
- node- changed node
 
- 
getPathToRootBuilds 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
- Returns:
- an array of TreeNodes giving the path from the root
 
- 
getPathToRootBuilds 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
 
- 
addTreeModelListenerAdds a listener for the TreeModelEvent posted after the tree changes.- Specified by:
- addTreeModelListenerin interface- TreeModel
- Parameters:
- l- the listener to add
- See Also:
 
- 
removeTreeModelListenerRemoves a listener previously added with addTreeModelListener().- Specified by:
- removeTreeModelListenerin interface- TreeModel
- Parameters:
- l- the listener to remove
- See Also:
 
- 
getTreeModelListenersReturns an array of all the tree model listeners registered on this model.- Returns:
- all of this model's TreeModelListeners or an empty array if no tree model listeners are currently registered
- Since:
- 1.4
- See Also:
 
- 
fireTreeNodesChangedprotected 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- the source of the- TreeModelEvent; typically- this
- path- the path to the parent of the nodes that changed; use- nullto identify the root has changed
- childIndices- the indices of the changed elements
- children- the changed elements
 
- 
fireTreeNodesInsertedprotected 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- the source of the- TreeModelEvent; typically- this
- path- the path to the parent the nodes were added to
- childIndices- the indices of the new elements
- children- the new elements
 
- 
fireTreeNodesRemovedprotected 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- the source of the- TreeModelEvent; typically- this
- path- the path to the parent the nodes were removed from
- childIndices- the indices of the removed elements
- children- the removed elements
 
- 
fireTreeStructureChangedprotected 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- the source of the- TreeModelEvent; typically- this
- path- the path to the parent of the structure that has changed; use- nullto identify the root has changed
- childIndices- the indices of the affected elements
- children- the affected elements
 
- 
getListenersReturns an array of all the objects currently registered asFooListeners upon this model.FooListeners are registered using theaddFooListenermethod.You can specify the listenerTypeargument with a class literal, such asFooListener.class. For example, you can query aDefaultTreeModelmfor 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.- Type Parameters:
- T- the listener type
- Parameters:
- listenerType- the type of listeners requested
- Returns:
- an array of all objects registered as
          FooListeners on this component, or an empty array if no such listeners have been added
- Throws:
- ClassCastException- if- listenerTypedoesn't specify a class or interface that implements- java.util.EventListener
- Since:
- 1.3
- See Also:
 
 
-