public class DefaultTreeModel extends Objectimplements 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 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 and Description |
|---|
DefaultTreeModel(TreeNode
Creates a tree in which any node can have children.
|
DefaultTreeModel(TreeNode
Creates a tree specifying whether any node can have children, or whether only certain nodes can have children.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addTreeModelListener(TreeModelListener
Adds a listener for the TreeModelEvent posted after the tree changes.
|
boolean |
asksAllowsChildren()
Tells how leaf nodes are determined.
|
protected void |
fireTreeNodesChanged(Object
Notifies all listeners that have registered interest for notification on this event type.
|
protected void |
fireTreeNodesInserted(Object
Notifies all listeners that have registered interest for notification on this event type.
|
protected void |
fireTreeNodesRemoved(Object
Notifies all listeners that have registered interest for notification on this event type.
|
protected void |
fireTreeStructureChanged(Object
Notifies all listeners that have registered interest for notification on this event type.
|
Object |
getChild(Object
Returns the child of
parent at index
index in the parent's child array.
|
int |
getChildCount(Object
Returns the number of children of
parent.
|
int |
getIndexOfChild(Object
Returns the index of child in parent.
|
<T extends EventListener |
getListeners(Class
Returns an array of all the objects currently registered as
FooListeners upon this model.
|
TreeNode |
getPathToRoot(TreeNode
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
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(MutableTreeNode
Invoked this to insert newChild at location index in parents children.
|
boolean |
isLeaf(Object
Returns whether the specified node is a leaf node.
|
void |
nodeChanged(TreeNode
Invoke this method after you've changed how node is to be represented in the tree.
|
void |
nodesChanged(TreeNode
Invoke this method after you've changed how the children identified by childIndicies are to be represented in the tree.
|
void |
nodeStructureChanged(TreeNode
Invoke this method if you've totally changed the children of node and its children's children...
|
void |
nodesWereInserted(TreeNode
Invoke this method after you've inserted some TreeNodes into node.
|
void |
nodesWereRemoved(TreeNode
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
Invoke this method if you've modified the
TreeNodes upon which this model depends.
|
void |
removeNodeFromParent(MutableTreeNode
Message this to remove node from its parent.
|
void |
removeTreeModelListener(TreeModelListener
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
Sets the root to
root.
|
void |
valueForPathChanged(TreePath
This sets the user object of the TreeNode identified by path and posts a node changed.
|
protected TreeNoderoot
protected EventListenerListlistenerList
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(value="root") public DefaultTreeModel(TreeNoderoot)
root - a TreeNode object that is the root of the tree
DefaultTreeModel(TreeNode, boolean)
public DefaultTreeModel(TreeNoderoot, boolean asksAllowsChildren)
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
asksAllowsChildren
public void setAsksAllowsChildren(boolean newValue)
public boolean asksAllowsChildren()
asksAllowsChildren
public void setRoot(TreeNoderoot)
root. A null
root implies the tree is to display nothing, and is legal.
public ObjectgetRoot()
public int getIndexOfChild(Objectparent, Object child)
null, returns -1.
getIndexOfChild in interface
TreeModel
parent - a note in the tree, obtained from this data source
child - the node we are interested in
null
public ObjectgetChild(Object parent, int index)
public int getChildCount(Objectparent)
getChildCount in interface
TreeModel
parent - a node in the tree, obtained from this data source
public boolean isLeaf(Objectnode)
askAllowsChildren setting.
isLeaf in interface
TreeModel
node - the node to check
asksAllowsChildren,
TreeModel.isLeaf(java.lang.Object)
public void reload()
TreeNodes upon which this model depends. The model will notify all of its listeners that the model has changed.
public void valueForPathChanged(TreePathpath, Object newValue)
valueForPathChanged in interface
TreeModel
path - path to the node that the user has altered
newValue - the new value from the TreeCellEditor
public void insertNodeInto(MutableTreeNodenewChild, MutableTreeNode parent, int index)
public void removeNodeFromParent(MutableTreeNodenode)
public void nodeChanged(TreeNodenode)
public void reload(TreeNodenode)
TreeNodes 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 changed
public void nodesWereInserted(TreeNodenode, int[] childIndices)
public void nodesWereRemoved(TreeNodenode, int[] childIndices, Object [] removedChildren)
public void nodesChanged(TreeNodenode, int[] childIndices)
public void nodeStructureChanged(TreeNodenode)
public TreeNode[] getPathToRoot(TreeNode aNode)
aNode - the TreeNode to get the path for
protected TreeNode[] getPathToRoot(TreeNode aNode, int depth)
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
public void addTreeModelListener(TreeModelListenerl)
addTreeModelListener in interface
TreeModel
l - the listener to add
removeTreeModelListener(javax.swing.event.TreeModelListener)
public void removeTreeModelListener(TreeModelListenerl)
removeTreeModelListener in interface
TreeModel
l - the listener to remove
addTreeModelListener(javax.swing.event.TreeModelListener)
public TreeModelListener[] getTreeModelListeners()
TreeModelListeners or an empty array if no tree model listeners are currently registered
addTreeModelListener(javax.swing.event.TreeModelListener),
removeTreeModelListener(javax.swing.event.TreeModelListener)
protected void fireTreeNodesChanged(Objectsource, 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 changed
childIndices - the indices of the changed elements
children - the changed elements
protected void fireTreeNodesInserted(Objectsource, Object [] path, int[] childIndices, Object [] children)
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
protected void fireTreeNodesRemoved(Objectsource, Object [] path, int[] childIndices, Object [] children)
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
protected void fireTreeStructureChanged(Objectsource, 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 changed
childIndices - the indices of the affected elements
children - the affected elements
public <T extends EventListener> T[] getListeners(Class <T> listenerType)
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.
listenerType - the type of listeners requested; this parameter should specify an interface that descends from
java.util.EventListener
FooListeners on this component, or an empty array if no such listeners have been added
ClassCastException - if
listenerType doesn't specify a class or interface that implements
java.util.EventListener
getTreeModelListeners()