aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Removing rows from a default table model Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Removing rows from a default table model" Watch "Removing rows from a default table model" New topic
Author

Removing rows from a default table model

Fabio Piergentili
Ranch Hand

Joined: Sep 04, 2009
Posts: 57
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Sep 04, 2009
Posts: 57
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

Joined: Sep 04, 2009
Posts: 57
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

Joined: Jun 13, 2009
Posts: 2172
    
    7
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

Joined: Sep 04, 2009
Posts: 57
Much nicer thanks for the help!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Removing rows from a default table model