DefaultTreeModel (Java 2 Platform SE 5.0) (original) (raw)
javax.swing.tree
Class DefaultTreeModel
java.lang.Object
javax.swing.tree.DefaultTreeModel
All Implemented Interfaces:
public class DefaultTreeModel
extends Object
implements 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 JavaBeansTM has been added to the java.beans
package. Please see XMLEncoder.
Field Summary | |
---|---|
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 |
---|
DefaultTreeModel(TreeNode root) Creates a tree in which any node can have children. |
[DefaultTreeModel](../../../javax/swing/tree/DefaultTreeModel.html#DefaultTreeModel%28javax.swing.tree.TreeNode, boolean%29)(TreeNode root, boolean asksAllowsChildren) Creates a tree specifying whether any node can have children, or whether only certain nodes can have children. |
Method Summary | |
---|---|
void | addTreeModelListener(TreeModelListener l) Adds a listener for the TreeModelEvent posted after the tree changes. |
boolean | asksAllowsChildren() Tells how leaf nodes are determined. |
protected void | [fireTreeNodesChanged](../../../javax/swing/tree/DefaultTreeModel.html#fireTreeNodesChanged%28java.lang.Object, java.lang.Object[], int[], java.lang.Object[]%29)(Object source,Object[] path, int[] childIndices,Object[] children) Notifies all listeners that have registered interest for notification on this event type. |
protected void | [fireTreeNodesInserted](../../../javax/swing/tree/DefaultTreeModel.html#fireTreeNodesInserted%28java.lang.Object, java.lang.Object[], int[], java.lang.Object[]%29)(Object source,Object[] path, int[] childIndices,Object[] children) Notifies all listeners that have registered interest for notification on this event type. |
protected void | [fireTreeNodesRemoved](../../../javax/swing/tree/DefaultTreeModel.html#fireTreeNodesRemoved%28java.lang.Object, java.lang.Object[], int[], java.lang.Object[]%29)(Object source,Object[] path, int[] childIndices,Object[] children) Notifies all listeners that have registered interest for notification on this event type. |
protected void | [fireTreeStructureChanged](../../../javax/swing/tree/DefaultTreeModel.html#fireTreeStructureChanged%28java.lang.Object, java.lang.Object[], int[], java.lang.Object[]%29)(Object source,Object[] path, int[] childIndices,Object[] children) Notifies all listeners that have registered interest for notification on this event type. |
Object | [getChild](../../../javax/swing/tree/DefaultTreeModel.html#getChild%28java.lang.Object, int%29)(Object parent, int index) Returns the child of parent at index index in the parent's child array. |
int | getChildCount(Object parent) Returns the number of children of parent. |
int | [getIndexOfChild](../../../javax/swing/tree/DefaultTreeModel.html#getIndexOfChild%28java.lang.Object, java.lang.Object%29)(Object parent,Object child) Returns the index of child in parent. |
<T extends EventListener> T[] | getListeners(Class listenerType) Returns an array of all the objects currently registered as _Foo_Listeners 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](../../../javax/swing/tree/DefaultTreeModel.html#getPathToRoot%28javax.swing.tree.TreeNode, int%29)(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. |
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](../../../javax/swing/tree/DefaultTreeModel.html#insertNodeInto%28javax.swing.tree.MutableTreeNode, javax.swing.tree.MutableTreeNode, int%29)(MutableTreeNode newChild,MutableTreeNode parent, int index) Invoked this to insert newChild at location index in parents children. |
boolean | isLeaf(Object node) Returns whether the specified node is a leaf node. |
void | nodeChanged(TreeNode node) Invoke this method after you've changed how node is to be represented in the tree. |
void | [nodesChanged](../../../javax/swing/tree/DefaultTreeModel.html#nodesChanged%28javax.swing.tree.TreeNode, int[]%29)(TreeNode node, int[] childIndices) Invoke this method after you've changed how the children identified by childIndicies are to be represented in the tree. |
void | nodeStructureChanged(TreeNode node) Invoke this method if you've totally changed the children of node and its childrens children... |
void | [nodesWereInserted](../../../javax/swing/tree/DefaultTreeModel.html#nodesWereInserted%28javax.swing.tree.TreeNode, int[]%29)(TreeNode node, int[] childIndices) Invoke this method after you've inserted some TreeNodes into node. |
void | [nodesWereRemoved](../../../javax/swing/tree/DefaultTreeModel.html#nodesWereRemoved%28javax.swing.tree.TreeNode, int[], java.lang.Object[]%29)(TreeNode node, int[] childIndices,Object[] removedChildren) Invoke this method after you've removed some TreeNodes from node. |
void | reload() Invoke this method if you've modified the TreeNodes upon which this model depends. |
void | reload(TreeNode node) Invoke this method if you've modified the TreeNodes upon which this model depends. |
void | removeNodeFromParent(MutableTreeNode node) Message this to remove node from its parent. |
void | removeTreeModelListener(TreeModelListener l) Removes a listener previously added with addTreeModelListener(). |
void | setAsksAllowsChildren(boolean newValue) Sets whether or not to test leafness by asking getAllowsChildren() or isLeaf() to the TreeNodes. |
void | setRoot(TreeNode root) Sets the root to root. |
void | [valueForPathChanged](../../../javax/swing/tree/DefaultTreeModel.html#valueForPathChanged%28javax.swing.tree.TreePath, java.lang.Object%29)(TreePath path,Object newValue) This sets the user object of the TreeNode identified by path and posts a node changed. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, [wait](../../../java/lang/Object.html#wait%28long, int%29) |
Field Detail |
---|
root
protected TreeNode root
Root of the tree.
listenerList
protected EventListenerList listenerList
Listeners.
asksAllowsChildren
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(java.lang.Object), setAsksAllowsChildren(boolean)
Constructor Detail |
---|
DefaultTreeModel
public DefaultTreeModel(TreeNode root)
Creates a tree in which any node can have children.
Parameters:
root
- a TreeNode object that is the root of the tree
See Also:
[DefaultTreeModel(TreeNode, boolean)](../../../javax/swing/tree/DefaultTreeModel.html#DefaultTreeModel%28javax.swing.tree.TreeNode, boolean%29)
DefaultTreeModel
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
- 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 Detail |
---|
setAsksAllowsChildren
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.
asksAllowsChildren
public 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:
setRoot
public void setRoot(TreeNode root)
Sets the root to root
. A null root
implies the tree is to display nothing, and is legal.
getRoot
public Object getRoot()
Returns the root of the tree. Returns null only if the tree has no nodes.
Specified by:
[getRoot](../../../javax/swing/tree/TreeModel.html#getRoot%28%29)
in interface [TreeModel](../../../javax/swing/tree/TreeModel.html "interface in javax.swing.tree")
Returns:
the root of the tree
getIndexOfChild
public int getIndexOfChild(Object parent, Object child)
Returns the index of child in parent. If either the parent or child is null
, returns -1.
Specified by:
[getIndexOfChild](../../../javax/swing/tree/TreeModel.html#getIndexOfChild%28java.lang.Object, java.lang.Object%29)
in interface [TreeModel](../../../javax/swing/tree/TreeModel.html "interface in javax.swing.tree")
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
getChild
public Object getChild(Object parent, int index)
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)).
Specified by:
[getChild](../../../javax/swing/tree/TreeModel.html#getChild%28java.lang.Object, int%29)
in interface [TreeModel](../../../javax/swing/tree/TreeModel.html "interface in javax.swing.tree")
Parameters:
parent
- a node in the tree, obtained from this data source
Returns:
the child of parent at index index
getChildCount
public int getChildCount(Object parent)
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.
Specified by:
[getChildCount](../../../javax/swing/tree/TreeModel.html#getChildCount%28java.lang.Object%29)
in interface [TreeModel](../../../javax/swing/tree/TreeModel.html "interface in javax.swing.tree")
Parameters:
parent
- a node in the tree, obtained from this data source
Returns:
the number of children of the node parent
isLeaf
public boolean isLeaf(Object node)
Returns whether the specified node is a leaf node. The way the test is performed depends on theaskAllowsChildren
setting.
Specified by:
[isLeaf](../../../javax/swing/tree/TreeModel.html#isLeaf%28java.lang.Object%29)
in interface [TreeModel](../../../javax/swing/tree/TreeModel.html "interface in javax.swing.tree")
Parameters:
node
- the node to check
Returns:
true if the node is a leaf node
See Also:
asksAllowsChildren, TreeModel.isLeaf(java.lang.Object)
reload
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.
valueForPathChanged
public void valueForPathChanged(TreePath path, Object newValue)
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.
Specified by:
[valueForPathChanged](../../../javax/swing/tree/TreeModel.html#valueForPathChanged%28javax.swing.tree.TreePath, java.lang.Object%29)
in interface [TreeModel](../../../javax/swing/tree/TreeModel.html "interface in javax.swing.tree")
Parameters:
path
- path to the node that the user has altered
newValue
- the new value from the TreeCellEditor
insertNodeInto
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.
removeNodeFromParent
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.
nodeChanged
public void nodeChanged(TreeNode node)
Invoke this method after you've changed how node is to be represented in the tree.
reload
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 node node
(PENDING).
nodesWereInserted
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.
nodesWereRemoved
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.
nodesChanged
public void nodesChanged(TreeNode node, int[] childIndices)
Invoke this method after you've changed how the children identified by childIndicies are to be represented in the tree.
nodeStructureChanged
public void nodeStructureChanged(TreeNode node)
Invoke this method if you've totally changed the children of node and its childrens children... This will post a treeStructureChanged event.
getPathToRoot
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
- the TreeNode to get the path for
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
addTreeModelListener
public void addTreeModelListener(TreeModelListener l)
Adds a listener for the TreeModelEvent posted after the tree changes.
Specified by:
[addTreeModelListener](../../../javax/swing/tree/TreeModel.html#addTreeModelListener%28javax.swing.event.TreeModelListener%29)
in interface [TreeModel](../../../javax/swing/tree/TreeModel.html "interface in javax.swing.tree")
Parameters:
l
- the listener to add
See Also:
removeTreeModelListener(javax.swing.event.TreeModelListener)
removeTreeModelListener
public void removeTreeModelListener(TreeModelListener l)
Removes a listener previously added with addTreeModelListener().
Specified by:
[removeTreeModelListener](../../../javax/swing/tree/TreeModel.html#removeTreeModelListener%28javax.swing.event.TreeModelListener%29)
in interface [TreeModel](../../../javax/swing/tree/TreeModel.html "interface in javax.swing.tree")
Parameters:
l
- the listener to remove
See Also:
addTreeModelListener(javax.swing.event.TreeModelListener)
getTreeModelListeners
public TreeModelListener[] getTreeModelListeners()
Returns an array of all the tree model listeners registered on this model.
Returns:
all of this model's TreeModelListener
s or an empty array if no tree model listeners are currently registered
Since:
1.4
See Also:
addTreeModelListener(javax.swing.event.TreeModelListener), removeTreeModelListener(javax.swing.event.TreeModelListener)
fireTreeNodesChanged
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
- the node being changed
path
- the path to the root node
childIndices
- the indices of the changed elements
children
- the changed elements
See Also:
fireTreeNodesInserted
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
- the node where new elements are being inserted
path
- the path to the root node
childIndices
- the indices of the new elements
children
- the new elements
See Also:
fireTreeNodesRemoved
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
- the node where elements are being removed
path
- the path to the root node
childIndices
- the indices of the removed elements
children
- the removed elements
See Also:
fireTreeStructureChanged
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
- the node where the tree model has changed
path
- the path to the root node
childIndices
- the indices of the affected elements
children
- the affected elements
See Also:
getListeners
public <T extends EventListener> T[] getListeners(Class listenerType)
Returns an array of all the objects currently registered as _Foo_Listener
s upon this model._Foo_Listener
s are registered using theadd_Foo_Listener
method.
You can specify the listenerType
argument with a class literal, such as_Foo_Listener.class
. For example, you can query aDefaultTreeModel
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:
listenerType
- the type of listeners requested; this parameter should specify an interface that descends fromjava.util.EventListener
Returns:
an array of all objects registered as_Foo_Listener
s on this component, or an empty array if no such listeners have been added
Throws:
[ClassCastException](../../../java/lang/ClassCastException.html "class in java.lang")
- if listenerType
doesn't specify a class or interface that implementsjava.util.EventListener
Since:
1.3
See Also:
Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.