Problem with getTreeCellRendererComponent for DefaultTreeCellRenderer
I have a tree say
a
a1(child node of a)
a2(child node of a)
b
c
a,b,c are sibling nodes whose parent is the root.
I need to change the text color of a1 and a2 nodes based on some conditions.
So I inherit class say MyRenderer from DefaultTreeCellRenderer, and use the getTreeCellRendererComponent. Here I check the "value" object argument for the condition. I expect a1 and a2 to get a different text color, but when I run my app a2 and b node's text has the color change. Any ideas why ? I have crossed checked the way I check my conditions, that is definitely correct.
I dont think I need to put the code snippet because it can be seen from "How to use Trees" in the Java tutorials for 1.4.2
Still here is my code -
class MyRenderer extends javax.swing.tree.DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(
tree, value, sel,
expanded, leaf, row,
hasFocus);
// Check conditions
if( condition==true ){
Color color1 = new Color(0, 0, 0xff);
setTextSelectionColor(color1);
setTextNonSelectionColor(color1);
}
else{
Color color2 = new Color(0xff, 0, 0);
setTextSelectionColor(color2);
setTextNonSelectionColor(color2);
}
return this;
}
}
-
DefaultTreeCellRenderer renderer = new MyRenderer;
JTree tree = new JTree;
tree.setCellRenderer(renderer);
Any ideas ?

