19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

«interface»<br />

javax.swing.tree.TreeModel<br />

+getChild(parent: Object, index: int): Object<br />

+getChildCount(parent: Object): int<br />

+getIndexOfChild(parent: Object, child: Object): int<br />

+getRoot(): Object<br />

+isLeaf(node: Object): boolean<br />

+addTreeModelListener(listener:<br />

TreeModelListener): void<br />

+removeTreeModelListener(listener:<br />

TreeModelListener): void<br />

+valueForPathChanged(path: TreePath, newValue:<br />

Object): void<br />

Returns the child of the parent at the index in the parent's child array.<br />

Returns the number of children of the specified parent in the tree model.<br />

Returns the index of the child in the parent. If the parent or child is null, returns –1.<br />

Returns the root of the tree. Returns null if the tree is empty.<br />

Returns true if the specified node is a leaf.<br />

Adds a listener for the TreeModelEvent posted after the tree changes.<br />

Removes a listener previously added with addTreeModelListener.<br />

Messaged when the user has altered the value for the item identified by path <strong>to</strong><br />

newValue.<br />

javax.swing.tree.DefaultTreeModel<br />

#asksAllowsChildren: boolean<br />

#root: TreeNode<br />

+DefaultTreeModel(root: TreeNode)<br />

+DefaultTreeModel(root: TreeNode,<br />

asksAllowsChildren: boolean)<br />

+asksAllowsChildren(): boolean<br />

+getPathToRoot(aNode: TreeNode): TreeNode[]<br />

+insertNodeIn<strong>to</strong>(newChild: MutableTreeNode,<br />

parent: MutableTreeNode, index: int): void<br />

+reload(): void<br />

+removeNodeFromParent(node: MutableTreeNode):<br />

void<br />

Tells how leaf nodes are determined. True if only nodes that do not allow<br />

children are leaf nodes, false if nodes that have no children are leaf nodes.<br />

The root of the tree.<br />

Creates a DefaultTreeModel with the specified root.<br />

Creates a DefaultTreeModel with the specified root and decides whether a<br />

node is a leaf node in the specified manner.<br />

Returns asksAllowsChildren.<br />

Returns the nodes in an array from root <strong>to</strong> the specified node.<br />

Inserts newChild at location index in parent’s children.<br />

Reloads the model (invoke this method if the tree has been modified).<br />

Removes the node from its parent.<br />

Figure 40.22<br />

TreeModel represents an entire tree and DefaultTreeModel is a concrete<br />

implementation of it.<br />

Once a tree is created, you can obtain its tree model using the<br />

getModel method. Listing 40.13 gives an example that traverses<br />

all the nodes in a tree using the tree model. Line 1 creates a<br />

tree using JTree’s no-arg construc<strong>to</strong>r with the default sample<br />

nodes, as shown in Figure 40.18. The tree model for the tree is<br />

obtained in line 4. Line 5 invokes the traversal method <strong>to</strong><br />

traverse the nodes in the tree.<br />

Listing 40.13 TestTreeModel.java<br />

<br />

<br />

<br />

<br />

<br />

<br />

1 public class TestTreeModel {<br />

2 public static void main(String[] args) {<br />

3 javax.swing.JTree jTree1 = new javax.swing.JTree();<br />

36

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!