Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable not removing rows properly

 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your SSCCE that demonstrates the problem.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out ListSelectionEvent#getValueIsAdjusting
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic