File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable not removing rows properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable not removing rows properly" Watch "JTable not removing rows properly" New topic
Author

JTable not removing rows properly

Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Dear friends,

I have a JTable called partTbl with DefaultTableModel. However, I have attached the following listener is attached to it:


The above listener makes sure that whenever a user preses tab and the cell gets focus it automatically comes to the editing mode which was what I wanted.

However, when I try to remove a row this listener interferes with it and un-intended rows get deleted. This I have verified, because when I remove the above
listener, the following code for removing the rows works very well, which along with the listener gives bizarre results:



Anand Shrivastava
SCJA
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19726
    
  20

Anand Shrivastava wrote:

Why are you increasing both currow and rowdeleted? Anyway, to delete multiple rows from a (table / list / ...) model, the safest way is to iterate backwards. That way the indexes of any row after the deleted one will not be modified:
If you iterate forward, removing row 2 will turn the old row 3 into row 2, the old row 4 into row 3, etc. You would need to adjust the index. If you iterate backwards you don't have this issue.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
That is why I have incremented rowdeleted to keep track of relative position of a row with respect to the loop after the row is deleted. The logic works pretty fine without that listener. As soon as I attach that listerner it starts giving bizarre result. I have even done that backward loop iteration. It also gives the same result. The problem is somewhere with the listener.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2206
    
    7
Post your SSCCE that demonstrates the problem.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18716
    
    8

It appears that deleting a row can cause a selection change event -- I would certainly expect that deleting the selected row would cause such an event, anyway. One remedy for that would be to turn off the selection listener while you are deleting rows.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10491
    
    9

Check out ListSelectionEvent#getValueIsAdjusting


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Dear friends,

After lot of mind boggling the thing which worked was
before iterating over the rows for removal

I add the lne

partTbl.removeEditor();

and everything works now
I wonder why.

But thanks to the Almighty and you guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable not removing rows properly