How do I put a JCheckBox into a cell of a JTable?


M

Mark Sizzler

When I google for "JTable JCheckbox cell" I get thousands of hits but they
do not explain on how to put a JCheckBox into all cells of a column and how
to retrieve the current status (checked/unchecked) from it.

As I understand I have to replace the current DefaultRenderer by
my own Renderer class. Currently my JTable is defined similar to:

myJTable = new JTable(myTableModel) {
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component component = super.prepareRenderer(renderer,row,column);
if (isRowSelected(row))
component.setBackground(Color.blue);
else if (((String)getValueAt(row,column)).equals("Test"))
component.setBackground(Color.yellow);
else
component.setBackground(Color.white);

return component; }
};

How can I extend this code with the necessary JCheckBox additions?

Mark
 
Ad

Advertisements

H

hiwa

When I google for "JTable JCheckbox cell" I get thousands of hits but they
do not explain on how to put a JCheckBox into all cells of a column and how
to retrieve the current status (checked/unchecked) from it.

As I understand I have to replace the current DefaultRenderer by
my own Renderer class. Currently my JTable is defined similar to:

myJTable = new JTable(myTableModel) {
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component component = super.prepareRenderer(renderer,row,column);
if (isRowSelected(row))
component.setBackground(Color.blue);
else if (((String)getValueAt(row,column)).equals("Test"))
component.setBackground(Color.yellow);
else
component.setBackground(Color.white);

return component; }
};

How can I extend this code with the necessary JCheckBox additions?

Mark

Renderer is useless because it is a rubber stamp on the screen,
not a real component.

See SwingSet2 source code that accompanies your JDK download.
 

Top