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