Reputation: 2859
I want to color(and give an icon to) a particular node of a JTree as opposed to in groups like OpenNode, LeafNode etc. How do I go about doing this?
Upvotes: 0
Views: 2670
Reputation: 54695
The easiest way to do this is to construct your JTree
model using DefaultMutableTreeNode
s and to set the "user object" value of certain node(s), and then use this value to determine the behaviour of the renderer when it encounters that node.
First we construct a simple model and pass it to the JTree
constructor:
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Hello");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Goodbye");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Bonjour");
root.add(child1);
root.add(child2);
JTree tree = new JTree(root);
Now define a custom tree cell renderer:
TreeCellRenderer renderer = new DefaultTreeCellRenderer() {
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
// Defer to superclass to create initial version of JLabel and then modify (below).
JLabel ret = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
// We know that value is a DefaultMutableTreeNode so this downcast is safe.
MutableTreeNode node = (MutableTreeNode) value;
// Inspect user object and change rendering based on this.
if ("Hello".equals(node.getUserObject())) {
ret.setIcon(...);
}
// Could also inspect whether node is a leaf node, etc.
return ret;
}
};
Upvotes: 0
Reputation: 170148
This tutorial from Sun shows how to set your own node-icons and how to differentiate between leaf- and non-leafs in a tree.
Upvotes: 2