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