Java Reference
In-Depth Information
Figur e 10•16. The ComponentT ree application
Example 10•20: ComponentTree.java (continued)
**/
public class ComponentTree extends JTree {
/**
* All this constructor method has to do is set the TreeModel and
* TreeCellRenderer objects for the tree. It is these classes (defined
* below) that do all the real work.
**/
public ComponentTree(Component c) {
super(new ComponentTreeModel(c));
setCellRenderer(new ComponentCellRenderer(getCellRenderer()));
}
/**
* The TreeModel class puts hierarchical data in a form that the JTree
* can display. This implementation interprets the containment hierarchy
* of a Component for display by the ComponentTree class. Note that any
* kind of Object can be a node in the tree, as long as the TreeModel knows
* how to handle it.
**/
static class ComponentTreeModel implements TreeModel {
Component root;
// The root object of the tree
// Constructor: just remember the root object
public ComponentTreeModel(Component root) { this.root = root; }
// Return the root of the tree
public Object getRoot() { return root; }
// Is this node a leaf? (Leaf nodes are displayed differently by JTree)
// Any node that isn't a container is a leaf, since they cannot have
// children. We also define containers with no children as leaves.
public boolean isLeaf(Object node) {
if (!(node instanceof Container)) return true;
Container c = (Container) node;
return c.getComponentCount() == 0;
}
// How many children does this node have?
Search WWH ::




Custom Search