Solved

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

Posted on 2000-02-14
1
878 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

623 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