Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Checkbox in JTable wont appear checked?  RSS feed

 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a checkbox in a JTable. This JTable uses an abstractTableModel with each row being populated by an arraylist containing Objects. One value is a boolean value that appears on the table as a checkbox. When I click on this checkbox. The value of the boolean in that particular object changes but its GUI representation does not check/uncheck itself to represent this. Is there something wrong with my setValueAt method in my custom table model?

 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the Swing tutorial on Creating a Table Model.

You can also see Row Table Model for an implementation that uses a List to hold the data. You basically just need to implement the getValueAt() and setValueAt() methods. The JButtonTableModel gives a short example of how to do this.
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I'm thick....

Turns out the problem was in my GetValueAt method. I had set the boolean value to false in a switch statement. This was during the stage where I just wanted the table to display and then forgot to add logic to it.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh oh! I see a Null Pointer exception if your rowList is empty
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Uh oh! I see a Null Pointer exception if your rowList is empty


The getValueAt method is only called when I pass data into the table and reload it, or am I missing something here? It isn't automatically loaded at the start.
 
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
The getValue() will be called whenever the table wants to repaint itself. From a developer's perspective that will be any time after the table is displayed on the UI
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:The getValue() will be called whenever the table wants to repaint itself. From a developer's perspective that will be any time after the table is displayed on the UI


Good point, I better make a cover for that. Thanks for the heads up.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!