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 ?

[1678 byte] By [ruchija] at [2007-10-3 9:19:58]
# 1
Use setForeground to set text color.
Andre_Uhresa at 2007-7-15 4:33:13 > top of Java-index,Desktop,Core GUI APIs...
# 2
setForeground does not help, because that will be used to change text color of ALL nodes in the JTree. My requirement is to change text color of SOME nodes in the JTree
ruchija at 2007-7-15 4:33:13 > top of Java-index,Desktop,Core GUI APIs...
# 3
It always worked for me with setForeground: first you set the default color that is used normally, and you overwrite the default with the special color only for the desired condition.
Andre_Uhresa at 2007-7-15 4:33:13 > top of Java-index,Desktop,Core GUI APIs...