I want write custom TreeCellRenderer to have Root, nodes and leafs in different color.
This is my code:
tree.setCellRenderer(new DefaultTreeCellRenderer() {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
if (node.isRoot()) {
super.setBackground(Color.red);
} else if (node.getChildCount() > 0) {
super.setBackground(Color.yellow);
} else if (leaf) {
super.setBackground(Color.green);
}
return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
}
}
});
Unfortunately only selected node changes color.
What am I doing wrong? TIA for help.
//update: I correlated my code, but it didn t help.