Java Reference
In-Depth Information
Figure 17-6.
A JTree with no connection lines
The following lines produce horizontal lines between level-one nodes:
UIManager.put("Tree.line", Color.GREEN);
JTree tree = new JTree();
tree.putClientProperty("JTree.lineStyle", "Horizontal");
Figure 17-7 shows how the horizontal lines appear.
Figure 17-7.
A JTree with horizontal level lines
TreeCellRenderer Interface
Each of the nodes within the
JTree
has an installed cell
renderer
. It's the responsibility of the
renderer to draw the node and clearly display its state. The default renderer is basically a
JLabel
, which allows you to have both text and an icon within the node. However, any compo-
nent can serve as the node renderer. The default renderer displays an icon that represents the
state of the node.
■
Note
The tree cell renderer is just that—a renderer. If the renderer were, say, a
JButton
, it wouldn't be
selectable but would nevertheless be drawn to look just like a
JButton
.
The configuration of each node renderer is defined by the
TreeCellRenderer
interface. Any
class implementing this interface can serve as a renderer for your
JTree
.
public interface TreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus);
}