• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Checkbox problem in JTable

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic