This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing rows from a default table model

 
Fabio Piergentili
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know how to remove rows from a default table model:

I tried

for ( int i=0; i<rowCount; i++ ) {
if ( (Boolean)dtm.getValueAt(i,16) )
dtm.removeRow(i);
}

This doesn't work as the index goes out of bounce. I think it is b/c when I remove a row the DTM has one less row. It felt wrong but then I tried

for ( int i=0; i<dtm.getRowCount(); i++ ) {
if ( (Boolean)dtm.getValueAt(i,16) )
dtm.removeRow(i);
}

This did not give an exception but the results were not consistent.

Does anyone have any suggestions?

Thanks
 
Paul Clapham
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fabio Piergentili wrote:I think it is b/c when I remove a row the DTM has one less row.

Yes, that's right. This problem always occurs when you are trying to remove entries from a list by their index; it has nothing to do with table models in particular.

Suppose you have entries numbered 0, 1, 2, 3, 4 in your list. When you remove the entry at index 0 (which is 0), now you have 1, 2, 3, 4. If you then remove the entry at index 1 (which is 2), you now have 1, 3, 4. You should have the picture by now so I won't go on.

So if you want to remove the entries at index 0, 1, and 2, what you should actually do is to remove the entry at index 0 three times. Alternatively, remove the entries at index 2, 1, and 0 (in that order) and you won't run into this problem.


 
Fabio Piergentili
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am removing based on if a value of a particular column is true or false ( random order ). I do not know ahead of time what I am deleting so you are telling me I have to figure what rows need to be deleted in one loop. Then in another loop, come up with a re-mapping scheme to delete the correct rows?

Thanks
 
Fabio Piergentili
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems to work ...

Boolean [] array = new Boolean [numberRows];
Vector rowsToRemove = new Vector();
if ( shipRadio.isSelected() ) {
for ( int i=0; i<numberRows; i++ ) {
if ( dtm.getValueAt(i,11) != null ) {
//array[i]=true;
rowsToRemove.add(i);
} else {
array[i]=false;
}
}

Iterator itr = rowsToRemove.iterator();
int i = 0;
while(itr.hasNext()) {
dtm.removeRow((Integer)itr.next() - i);
i++;
}
 
Rob Camick
Ranch Hand
Posts: 2539
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so you are telling me I have to figure what rows need to be deleted in one loop. Then in another loop, come up with a re-mapping scheme to delete the correct rows?


No, what Paul was saying is that you need to do the loop in reverse.

Instead of:


you use:



And use the "Code" tags so the code you post is readable.
 
Fabio Piergentili
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much nicer thanks for the help!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic