File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable not removing rows properly" Watch "JTable not removing rows properly" New topic

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
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

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.

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: 2454
Post your SSCCE that demonstrates the problem.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

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: 10828

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


and everything works now
I wonder why.

But thanks to the Almighty and you guys
I agree. Here's the link:
subject: JTable not removing rows properly
It's not a secret anymore!