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.

Figure 40.18<br />

JTree displays data in a treelike hierarchy.<br />

All the nodes displayed in the tree are in the form of a<br />

hierarchical indexed list. The tree can be used <strong>to</strong> navigate<br />

structured data with hierarchical relationships. A node can have<br />

child nodes. A node is called a leaf if it has no children; a<br />

node with no parent is called the root of its tree. A tree may<br />

consist of many subtrees, each node acting as the root for its<br />

own subtree.<br />

A nonleaf node can be expanded or collapsed by double-clicking on<br />

the node or on the node's handle in front of the node. The handle<br />

usually has a visible sign <strong>to</strong> indicate whether the node is<br />

expanded or collapsed. For example, on Windows, the + symbol<br />

indicates that the node can be expanded, and the – symbol, that<br />

it can be collapsed.<br />

Like JTable, JTree is a very <strong>com</strong>plex <strong>com</strong>ponent with many<br />

supporting interfaces and classes. JTree is in the javax.swing<br />

package, but its supporting interfaces and classes are all<br />

included in the javax.swing.tree package. The supporting<br />

interfaces are TreeModel, TreeSelectionModel, TreeNode, and<br />

MutableTreeNode, and the supporting classes are DefaultTreeModel,<br />

DefaultMutableTreeNode, DefaultTreeCellEdi<strong>to</strong>r,<br />

DefaultTreeCellRenderer, and TreePath.<br />

While JTree displays the tree, the data representation of the<br />

tree is handled by TreeModel, TreeNode, and TreePath. TreeModel<br />

represents the entire tree, TreeNode represents a node, and<br />

TreePath represents a path <strong>to</strong> a node. Unlike the ListModel or<br />

TableModel, TreeModel does not directly s<strong>to</strong>re or manage tree<br />

data. Tree data are s<strong>to</strong>red and managed in TreeNode and TreePath.<br />

DefaultTreeModel is a concrete implementation of TreeModel.<br />

MutableTreeNode is a subinterface of TreeNode, which represents a<br />

tree node that can be mutated by adding or removing child nodes,<br />

or by changing the contents of a user object s<strong>to</strong>red in the node.<br />

The TreeSelectionModel interface handles tree node selection. The<br />

DefaultTreeCellRenderer class provides a default tree node<br />

renderer that can display a label and/or an icon in a node. The<br />

DefaultTreeCellEdi<strong>to</strong>r can be used <strong>to</strong> edit the cells in a text<br />

field.<br />

A TreePath is an array of Objects that are vended from a<br />

TreeModel. The elements of the array are ordered such that the<br />

30

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

Saved successfully!

Ooh no, something went wrong!