• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

TableCellRenderer and JCheckBox

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using a boolean value as a column within a JTable. I want to change the color of a JCheckBox from black to red in the column based on a test.
I created a class to extend JCheckBox which implements TableCellRenderer. When the JTable is displayed, the checkbox is there, but you can't see the check in the box until you press the mouse key on the checkbox.
Here's the setup of the JTable:
TableName.setDefaultRenderer(Boolean.class,
new gridTableCellRenderer(DisplayData));

If I comment out the line above, the checkbox appears correctly showing the checked or unchecked condition without depressing the mousebutton.

Here's the class itself:
package NewPackage;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.util.*;

class gridTableCellRenderer extends JCheckBox implements TableCellRenderer {

private Vector DisplayData = null;
public gridTableCellRenderer( Vector DisplayData ) {
super();
// setOpaque(true);
this.DisplayData = DisplayData;
}
public Component getTableCellRendererComponent( JTable table,
Object value, boolean isSelected,
boolean hasFocus, int row, int column ) {
Display record = (Display) DisplayData.get(row);

if ( record.getItemID() == 0 ) {
switch( column ) {
case 0:
// setBackground(Color.red);
// setForeground(Color.black);
// setEnabled(false);
break;
case 3:
// setForeground(Color.red);
break;

default:
break;
} /* ...end of switch statement... */
} /* ...end of if ( record.getItemID() == 0... */
else {
switch( column ) {
case 0:
// setBackground(Color.lightGray);
// setForeground(Color.black);
// setEnabled(true);
break;

case 3:
// setForeground(Color.black);
break;

default:
break;
} /* ...end of switch statement... */
} /* ...end of else statement... */
return this;
} /* ...end of method getTableCellRendererComponent()... */
} /* ...end of class declaration gridTableCellRenderer... */

As you can see, I've commented out all of the logic in an effort to try to find out what was causing the problem. As I said before, the only way I can get the checkboxes to appear correctly is commenting out the line:
TableName.setDefaultRenderer(Boolean.class,
new gridTableCellRenderer(DisplayData));

Thanks in advance....
 
reply
    Bookmark Topic Watch Topic
  • New Topic