Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2000-02-14
1
Medium Priority
?
890 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 70 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

916 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