Solved

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

Posted on 2000-02-14
1
851 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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now