Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Checkbox problem in JTable  RSS feed

 
Antony Patric
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a defaulttable model and I need to place a checkbox in one of the columns.I used this code below:

table.getColumnModel().getColumn(8).setCellEditor(new DefaultCellEditor(new JCheckBox()));

But I got the value as String in the columns cells and not as checkboxes.
Could someone help me?I need to have the real checkboxes instead of the String in the cells.

Regards,
Antony
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may wish to extend the DefaultTableModel class and in this extended class, override the getColumnClass method for the column that will contain the checkbox. I assume that this column will hold boolean data, and so your getColumnClass() should return Boolean.class when the proper index is passed. For instance, say that the first column contains boolean information, and you want to have a checkbox placed in this column, then your getcolumnclass override could look like so:
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you override the getColumnClass in your table model to return Boolean.class you do NOT need to explicitly provide a renderer to display a JCheckBox. Swing renders Booleans as JCheckboxes by default.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!