This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

TableCellRenderer and JCheckBox  RSS feed

 
Duane Riech
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!