• Post Reply Bookmark Topic Watch Topic
  • New Topic

Boolean value in JTable display problem after delete

 
K. Tsang
Bartender
Posts: 3604
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello does anyone use booleans in their JTable data?? I noticed for me anyway it displays the boolean value as a checkbox then when i try to delete the row it has a ClassCastException

"Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.Boolean"

from the JTable's getTableCellRendererComponent() method


Any suggestion to prevent this? Should I just use a String and display Y/N instead?

 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Helou,

Yes JTable has few default renderers and editors.

You told that you get this ClassCastException when you delete / have deleted the row ?
ummm...confused... When you are deleting this row do you have to still interact with it ?

And if the value is boolean and table's default Boolean renderer / editor is used then yes check box is displayed and editor returns Boolean value too, and it is Boolean. If you get ClassCastException then the value is not Boolean instance !

Debug with System.out.println();
 
K. Tsang
Bartender
Posts: 3604
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah thanks Mikko. In fact when I delete a row, that row's value becomes "java.lang.Object@...." when is probably String or Object, so when it gets to the Boolean column, it messed up. At least thats how I see it. If I changed my boolean to string I still get that row displayed. See screenshot.

So now I'm back to the problem of once deleted, how to ensure the table view render properly. I already used fireTableRowDeleted() but not seem to work correctly. Do I need to override this method?

My code look like this

screenshot.jpg
[Thumbnail for screenshot.jpg]
table view after row deleted
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:

Regardless, that check could be shorter:

If value is null or its booleanValue() is false, Boolean.TRUE.equals(value) returns false.

It will also handle the cases where value is not a Boolean for any reason, but you still should make sure it is.
 
K. Tsang
Bartender
Posts: 3604
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah I figured out why I got the ClassCastException. It was because of my TableModel's getValueAt() method. Now I don't get that exception.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!