Table cell renderer

J

John M

Hello,

I have a problem with a cell renderer. I want to make a column with
checkboxes, and I write the following code. When I edit the cell it use a
checkbox editor, but if I leave the cell it returns to a text (true/false).
What can I do?

model.addColumn("Col1");
model.insertRow(0,new Boolean[]{new Boolean(false)});
jTable1.getColumnModel().getColumn(0).setCellEditor(new
DefaultCellEditor(new JCheckBox()));
jTable1.getColumnModel().getColumn(0).setCellRenderer(new
DefaultTableCellRenderer());

Thanks!
 
C

Christian Kaufhold

Please use comp.lang.java.gui for GUI questions.

I have a problem with a cell renderer. I want to make a column with
checkboxes, and I write the following code. When I edit the cell it use a
checkbox editor, but if I leave the cell it returns to a text (true/false).
What can I do?

DefaultTableCellRenderer is not suitable for displaying boolean values
as a checkbox. The only standard renderer that is is JTable's default
renderer for Boolean.class.

model.addColumn("Col1");

DefaultTableModel.addColumn has usually undesirable side-effects,
(destroying the current column model settings). It is best to
*start* with the current number of columns if known in advance.

model.insertRow(0,new Boolean[]{new Boolean(false)});
jTable1.getColumnModel().getColumn(0).setCellEditor(new
DefaultCellEditor(new JCheckBox()));
jTable1.getColumnModel().getColumn(0).setCellRenderer(new
DefaultTableCellRenderer());

jTable1.getColumnModel().getColumn(0).setCellRenderer
(jTable1.getDefaultRenderer(Boolean.class));


Alternatively, just rely on the column-class-based default renderers/
editors and make getColumnClass(0) == Boolean.class.



Christian
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top