Modify JTree Leaf(Single leaf) Background

How do I change a single leaf's background in a JTree? I.E.TreeL1L2L3Change L2's background color, but leave the rest alone.
[175 byte] By [tecknophreaka] at [2007-11-26 16:08:10]
# 1

I dont know if this is the best way, but look at:

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/tree/DefaultTreeCellRenderer.html#getTreeCellRendererComponent(javax.swing.JTree,%20java.lang.Object,%20boolean,%20boolean,%20boolean,%20int,%20boolean)

(the getTreeCellRendererComponent() method)

You can do an if statement like:

if( value == specialLeaf){

setBackground(Color.green);

}

Message was edited by:

Nether

Nethera at 2007-7-8 22:30:31 > top of Java-index,Desktop,Core GUI APIs...
# 2

When I do that, it doens't change the background color. I tried setBackgroundNonSelectionColor, which states that it changes the background for just that node, but it changes the background for all of the nodes.

i.e.:

if (value == myNode) {

System.out.println("Here's my node");

setBackgroundNonSelectionColor(Color.RED);

}

Here's the rest of the function, in case I'm calling something I shouldn't be:

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);

// above snippet goes here

return this;

}

tecknophreaka at 2007-7-8 22:30:31 > top of Java-index,Desktop,Core GUI APIs...
# 3

> I tried setBackgroundNonSelectionColor, which

> states that it changes the background for just that

> node, but it changes the background for all of the

> nodes.

How about you try this:

if(node == myNode){

setColor(red)

}

else{

setColor(blue)

}

What might be happening is that it sets the color to red, but then it never sets it back again. For instance, if you are drawing with Graphics, when you change the color with g.setColor(c), it will stay that color until another piece of code changes it back.

Hope this works.

Nethera at 2007-7-8 22:30:31 > top of Java-index,Desktop,Core GUI APIs...
# 4
Absolutely!!Thanks.
tecknophreaka at 2007-7-8 22:30:31 > top of Java-index,Desktop,Core GUI APIs...