Reputation: 1
in my Jtable I have a column with boolean values displayed as checkbox. I can retrive the value cell only when the value is true, when values i false I can't read the values. I write my code:
int row = jTMezziInt.getRowCount();
int h=0;
while (h<=row){
chk= ((Boolean)jTMezziInt.getValueAt(h, 6)).booleanValue();
//if chk is true I can read;
// if chk is false the execution stopped at the chk assignement;
if (chk)
((DefaultTableModel )this.jTMezziInt.getModel()).removeRow(h);
row = jTMezziInt.getRowCount();
h=h+1;
}
TableColumn Selez = jTMezziInt.getColumnModel().getColumn(6);
}
For define the table I had used netbeas with table editor.
Thanks everybody for help;
Upvotes: 0
Views: 4477
Reputation: 3379
There are one of two possible issues here from what I can see. The most likely issue is that you start with h = 0 and then end with h = row (since your while loop iterates while h <= row).
Say for example you have 3 rows in your table. This loop will now run for h = 0, h = 1, h = 2 and h = 3, i.e. it runs 4 times but you only have 3 rows (indexed 0, 1 and 2 there is no row with index 3). This would cause a null pointer exception on its final iteration. Is this the behaviour you are seeing?
To sort it out just make your while loop condition h < row, not h <= row. If this does not work then let me know and we can discuss the other possible issue.
Another issue is that you still increment h even if the row you are checking is deleted. If we are checking row 1 and then delete row 1 then row 2 will become row 1, so we need to recheck this new row 1. So you should only increment h if the current row you are checking is not deleted.
Finally, as an aside, note that if you are using one of the later JDKs there is no need to call booleanValue() (this is called unnecessary unboxing). This will be done automtically for you. So you can change the following:
chk= ((Boolean)jTMezziInt.getValueAt(h, 6)).booleanValue();
to:
chk= (Boolean)jTMezziInt.getValueAt(h, 6);
It's just neater and better style.
Upvotes: 2