Java Reference
In-Depth Information
public class TreeTips {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Tree Tips");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Properties props = System.getProperties();
JTree tree = new JTree(props);
ToolTipManager.sharedInstance().registerComponent(tree);
TreeCellRenderer renderer = new ToolTipTreeCellRenderer(props);
tree.setCellRenderer(renderer);
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Although this example creates a new tree cell renderer, the behavior is only customizing
what has already been done for the DefaultTreeCellRenderer . Instead of needing to configure
the icons and text yourself, let the default renderer do it for you. Then add the tooltip text.
Editing Tree Nodes
In addition to supporting individualized tree cell renderers, JTree components can be editable,
allowing users to change the contents of any node of the tree. By default, trees are read-only.
To make a tree editable, just change the editable property setting to return true :
aTree.setEditable(true);
By default, the editor is a text field. There is also built-in support for picking choices from
combo boxes or check boxes. If you prefer, you can create a customized editor for a tree, just as
you can create a custom cell renderer.
Note Unfortunately, the built-in check box editor works better within a table than within a tree, where the
column label is the name and the value is the cell.
Figure 17-12 shows a tree using the default editor. To enable the editor, select a node,
and then double-click it. If the node isn't a leaf node, selecting it will also display or hide the
node's children.
 
Search WWH ::




Custom Search