Selection background color in a custom TreeCellRenderer

I'm trying to write a custom TreeCellRender and I'm not having any luck getting the selection to work. When I click on a node (leaf or not) the color of the node does not change. If I use the default renderer instead of my custom renderer selection works fine. I've tried everything I can find on the web, setOpaque(true) setTextSelectionColor() etc but I'm not getting anywhere.I've also tried setting the background color in getTreeCellRendererComponent() based on the selected boolean. Still nothing.

Here's my code:

class PackageTreeRendererextends DefaultTreeCellRenderer{

public PackageTreeRenderer(){

setOpaque(true);

setTextSelectionColor(getBackgroundSelectionColor());

}

public Component getTreeCellRendererComponent(JTree t,Object value,boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus){

DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;

Object obj = node.getUserObject();

if(objinstanceof String){

String name = (String) obj;

setText(name);

}

if(objinstanceof ResultPackageInterface){

ResultPackageInterface rpi = (ResultPackageInterface) obj;

String name ="Name";

try{

name = rpi.getDescription();

}

catch(RemoteException ex){

errorMessage(ex);

}

setText(name);

}

if(leaf){

setIcon(getLeafIcon());

}

else{

if(expanded){

setIcon(getOpenIcon());

}

else{

setIcon(getClosedIcon());

}

}

returnthis;

}

}

Hope someone can help.

[2949 byte] By [sasdava] at [2007-11-26 16:07:48]
# 1

if you are just trying to change the background color of the selection,

the UIManager is what you want

put this line prior to creating a JTree()

UIManager.put("Tree.selectionBackground",

new javax.swing.plaf.ColorUIResource(Color.YELLOW));

Michael_Dunna at 2007-7-8 22:30:08 > top of Java-index,Desktop,Core GUI APIs...
# 2
Nope. Nice idea but it doesn't change anything.
sasdava at 2007-7-8 22:30:08 > top of Java-index,Desktop,Core GUI APIs...
# 3

Maybe this is too obvious, but you are not setting the background color anywhere. Add these lines to the getTreeCellRendererComponent

setBackground(sel ? UIManager.getColor("Tree.selectionBackground"): tree.getBackground());

setForeground(sel ? UIManager.getColor("Tree.selectionForeground"): tree.getForeground());

mpmarronea at 2007-7-8 22:30:08 > top of Java-index,Desktop,Core GUI APIs...
# 4
Actually I have tried that and every variation of it I can think of, still no luck.Next!
sasdava at 2007-7-8 22:30:08 > top of Java-index,Desktop,Core GUI APIs...
# 5
this other thread (today) has some stuff re colors - worth a look http://forum.java.sun.com/thread.jspa?threadID=5128528
Michael_Dunna at 2007-7-8 22:30:08 > top of Java-index,Desktop,Core GUI APIs...
# 6

Thanks for the suggestions everyone but I got it working. Instead of extending DefaultTreeCellRenderer it works if I extend JLabel and implement TreeCellRenderer. Here's my code for future reference:

class PackageTreeRenderer extends JLabel implements TreeCellRenderer {

public PackageTreeRenderer() {

setOpaque(true);

}

public Component getTreeCellRendererComponent(JTree t,Object value,boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus) {

DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;

Object obj = node.getUserObject();

setText(obj.toString());

if(obj instanceof String) {

String name = (String) obj;

setText(name);

}

if(obj instanceof ResultPackageInterface) {

ResultPackageInterface rpi = (ResultPackageInterface) obj;

String name = "Name";

try {

name = rpi.getDescription();

}

catch(RemoteException ex) {

errorMessage(ex);

}

setText(name);

}

if(leaf) {

setIcon(UIManager.getIcon("Tree.leafIcon"));

}

else {

if(expanded) {

setIcon(UIManager.getIcon("Tree.openIcon"));

}

else {

setIcon(UIManager.getIcon("Tree.closedIcon"));

}

}

if(sel) {

setForeground(UIManager.getColor("Tree.selectionForeground"));

setBackground(UIManager.getColor("Tree.selectionBackground"));

}

else {

setForeground(UIManager.getColor("Tree.textForeground"));

setBackground(UIManager.getColor("Tree.textBackground"));

}

return this;

}

}

sasdava at 2007-7-8 22:30:08 > top of Java-index,Desktop,Core GUI APIs...