Solved

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

Posted on 2000-02-14
1
865 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 69
ejb message driven bean mdb creation steps 2 34
Need Help! Getting a syntax error and don't understand why 3 38
JDBC performance 8 28
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

740 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