Solved

How to highlight a node of JTree with diffrent colour when node is selected

Posted on 2000-02-14
1
861 Views
Last Modified: 2012-08-14
How to highlight a node of JTree with diffrent colour when node is selected by mouse click.  As soon as mouse is clicked on the node, node should be selected (highlighted) with diffrent colour.

Siddhartha
0
Comment
Question by:bera
1 Comment
 
LVL 1

Accepted Solution

by:
fmaritato earned 35 total points
ID: 2519196
You have to make a customized cell renderer for your tree.
I have attached an example for you. Note that you must specify
a paint method, because simply using the setBackground method
for JLabel does not work. Also note that you can specify any color
you want by changing the SelectedBackgroundColor variable.
In your main program call tree.setCellRenderer(thisRenderer);

Example:

import com.sun.java.swing.JLabel;
import com.sun.java.swing.Icon;
import com.sun.java.swing.UIManager;
import com.sun.java.swing.JTree;
import com.sun.java.swing.tree.TreeCellRenderer;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Component;

public class CDSTreeCellRenderer extends JLabel
  implements TreeCellRenderer {
 
  // Folder Icons
  private Icon  closedIcon = UIManager.getIcon("Tree.openIcon");
  private Icon  openIcon   = UIManager.getIcon("Tree.closedIcon");

  // Is this node selected?
  private boolean selected;

  // Important Colors
  private Color SelectedBackgroundColor =
    UIManager.getColor("Tree.backgroundSelectionColor");
  private  Color SelectedForegroundColor =
    UIManager.getColor("Tree.textSelectionColor");  
  private  Color NonSelectedBackgroundColor =
    UIManager.getColor("Tree.backgroundNonSelectionColor");
  private  Color NonSelectedForegroundColor =
    UIManager.getColor("Tree.textNonSelectionColor");
  private  Color BorderSelectionColor =
    UIManager.getColor("Tree.borderSelectionColor");
 
  /**********************************************************************/
  public CDSTreeCellRenderer() {
   
  }

  /**********************************************************************/
  public Component getTreeCellRendererComponent(JTree   tree,
                                    Object  value,
                                    boolean selected,
                                    boolean expanded,
                                    boolean leaf,
                                    int     row,
                                    boolean hasFocus) {

    String stringValue  = tree.convertValueToText(value,
                                      selected,
                                      expanded,
                                      leaf,
                                      row,
                                      hasFocus);
    setText(stringValue);
    if (expanded) {
      setIcon(openIcon);
    } else {
      setIcon(closedIcon);
    }

    if(selected) {
      setForeground(SelectedForegroundColor);
    } else {
      setForeground(NonSelectedForegroundColor);
    }

    this.selected = selected;
   
    return this;
  }

  /**********************************************************************/
  public void paint (Graphics g) {
   
    if (selected) {
      g.setColor(SelectedBackgroundColor);
    } else {
      g.setColor(NonSelectedBackgroundColor);      
    }

    Icon currentI = getIcon();

    if (currentI != null && getText() != null) {
      int offset = (currentI.getIconWidth() + getIconTextGap());
      g.fillRect(offset,
             0,
             getWidth() - 1 - offset,
             getHeight() - 1);
    } else {
      g.fillRect(0,
             0,
             getWidth() - 1,
             getHeight() - 1);
    }
     
    if (selected) {
      g.setColor(BorderSelectionColor);
      g.drawRect(0,
             0,
             getWidth() - 1,
             getHeight() - 1);
    }

    super.paint(g);
  }
}
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 99
Weekend adv creator 3 39
netstat -ano | find "8000" and taskkill /f /pid 2984 3 58
Configure a Bean in an XML file 4 33
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question