Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Checkbox not displaying in renderer of custom AbstractTableModel  RSS feed

 
Beau Tremblay
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand for a simple table model, all that is required to show a Boolean as a checkbox is to 1) override getColumnClass to return a Boolean for that column and 2) override isCellEditable. I've run simple examples, and the checkbox appears fine.

I have a class called ResultSet used to store a collection of data which extends AbstractTableModel

In the table's view class, I have a method which sets up the table columns:

The problem is the checkbox is not appearing when rendered. I see "true" or "false". Here is my renderer factory:

If I add back the commented line, where a Boolean.class column uses the custom CheckBoxRender, it works. Though shouldn't the DefaultTableCellRenderer handle this case fine?

Thanks for any insight.
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- JTable has built_in support for JCheckBox as Renderer and Editor by getColumnClass with Boolean.Class

- result from setupColumns() should be generating

pseudo code



- another terra incognita is TableCellRenderer, please how they works

- for better help sooner post an SSCCE, short, runnable, compilable with hardcoded value for XxxTableModel
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is the checkbox is not appearing when rendered. I see "true" or "false". Here is my renderer factory:

Your TableCellRendererFactory returns a DefaultTableCellRenderer for the general case, and its calling code sets that renderer/editor to the column. You really don't want to do that, as it removes the Boolean renderer/editor.

I think I would pass a TableColumn to a void method and set a custom renderer/editor only for classes for which I have one.

Note that Class does not override Object#equals(...) which performs an identity comparison. You can use == to compare class literals; there's only one instance of a Class object (except maybe in case of using multiple class loaders, in which case .equals(...) will also return false). If nothing else, it makes the code more readable.

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh and DefaultTableCellRenderer extends JLabel so will never show a checkbox. You can find the BooleanRenderer in JTable.java at or around line 5377
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Darryl Burke

Oh and ... this was excelent catch +++
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!