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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top