I think I have found a bug in the behavior of the TreeCellRenderer
setTextNonSelectionColor method. Instead of setting the text color for
the current cell, it sets the color of the following row upon
initialization and when the row to be colored is selected it will set
the text of the next row that is selected instead of the selected row.
Here is a sample applet generated by NetBeans:
/*
* TestColorsTree.java
*
* Created on August 15, 2003, 8:51 AM
*/
/**
*
* @author Jim Katz
*/
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.JTree;
public class TestColorsTree extends javax.swing.JApplet {
/** Initializes the applet TestColorsTree */
public void init() {
renderer = new defaultRenderer();
initComponents();
}
private defaultRenderer renderer = null;
/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
getContentPane().setLayout(null);
jTree1.setCellRenderer(renderer
);
jScrollPane1.setViewportView(jTree1);
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(82, 38, 160, 182);
}
public class defaultRenderer extends
javax.swing.tree.DefaultTreeCellRenderer {
public defaultRenderer(){
}
public java.awt.Component getTreeCellRendererComponent(
JTree jTree1,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(jTree1, value,
sel,expanded, leaf, row,hasFocus);
this.setTextNonSelectionColor(new java.awt.Color(0,0,0));
String valString = value.toString();
String red = "red";
if ( leaf ){
if (valString.equals(red)){
this.setBackgroundNonSelectionColor(new
java.awt.Color(255,0,0)); // Make cell blue
this.setTextNonSelectionColor( new
java.awt.Color(0,255,0)); // set text to yellow ( this cell ONLY!)
}
else {
this.setBackgroundNonSelectionColor(new
java.awt.Color(255,255,255));
}
}
else {
this.setBackgroundNonSelectionColor(new
java.awt.Color(255,255,255));
this.setTextNonSelectionColor(new
java.awt.Color(0,0,0));
}
return this;
}
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTree jTree1;
// End of variables declaration
}
Is there a flaw in my coding or should I report this as a Bug to Sun?
Thanks,
Jim
--
! Jim Katz
! Certified Clarion Developer
! Author: ABC DLL Toolkit Maintainer: DEF for ABC,DET for
Legacy
! Web Site: http://home.iag.net/~jimkatz/
! Company: iTradeZone,Inc.
! Using Clarion for Windows - Always looking for the simpler solution.