Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

The isSelected() of JCheckBox added to a cell of JTable not working properly

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I add a row and first column is a JCheckBox, if I select JCheckBox of even one row, the isSelected() of JCheckBox of all rows returns true.


JCheckBox for i=0 is not checked
JCheckBox for i=1is checked
JCheckBox for i=2 is not checked
Expected output is
false
true
false
Actual Output is
true
true
true
elect.JPG
[Thumbnail for elect.JPG]
actual form
 
Rancher
Posts: 3248
30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really sure what you are trying to do, but I would suggest:

1. you don't need a custom editor

2. if you want to know the state of any cell in the table then you use the getValueAt(...) method to get the data in the cell. The data in the cell should be a Boolean value, so you just check if the value is TRUE/FALSE.

3. if you want to perform some action when a cell is clicked then you can add a TableModelListener to the TableModel. An even will be generated then you check the state of the Boolean value and do your processing.
 
Author
Posts: 974
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to read a tutorial on how Swing's cell editors and cell renders are supposed to work. Typically the same editor object will be used and re-used no matter which row is being edited, which would explain the behavior you're seeing.

But, beyond that, you shouldn't really need to create a custom cell editor if you want your JTable to display checkboxes. There are built-in cell editors for that, so all you need to do is make sure your TableModel returns Boolean.class in its getColumnClass() method [and also make sure to actually return Boolean.TRUE or Boolean.FALSE in its getValueAt() method]. And if you want to know whether a given row is checked or not, you should not ask the cell editor (nor the cell renderer) but just ask the TableModel directly: myTable.getModel().getValueAt(row, column)

You haven't shown us how you're dealing with your TableModel, except that it's either DefaultTableModel or a subclass. In general I would recommend creating your own table model that extends AbstractTableModel rather than using DefaultTableModel for almost any use case. But that's irrelevant for this checkbox stuff, because either approach should work. However it's weird that your addRow() method creates a zero-length array to pass to DefaultTableModel.addRow(). Presumably your table model has at least one column, or else there would be no cells to have cell editors for.
 
Suma Gopalakrishna
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Rob and Brian. I was able to solve the problem by using (Boolean)mytable.getValueAt(i,0); I had used it earlier somehow didnt strike while debugging the present code. I have different instances of cell editors for different rows in JComboBox and works fine. Why should same cellEditor instance be used for JCheckBox. I feel there is a bug in java swings's JCheckBox. Using TableModel or Key Released Event of JTable are different ways of accessing and modifying the individual cells in JTable.
 
Rob Camick
Rancher
Posts: 3248
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why should same cellEditor instance be used for JCheckBox.


You can only edit a single cell at a time so you only ever need a single editor.  The default Boolean editor is designed to be generic so it can be used by all relevant cells.

I feel there is a bug in java swings's JCheckBox.


There is no bug, only your lack of understand of how renderers and editors are used to make efficient use of Swing resources by making the renderer/editor reusable.

Using TableModel or Key Released Event of JTable are different ways of accessing and modifying the individual cells in JTable.


No idea what you are trying to say. The TableModel contains data. You use the getVAlueAt(...) method to access the data.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic