• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deleting the selected row from a JTable

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to delete the selected row from a JTable and not understanding what I'm doing wrong. The row, in reality is being deleted but the displayed table is not right. Here is my delete code:

What happens after this execution is that the selected row remains highlighted (not necessarily bad but I really don't want it highlighted at all). What is odd is that column 1 retains the value of what should be the deleted row but column 2 contains the value of the next row down. That row remains in tact. In other words the row that is supposed to be deleted remains and is partially set to the next row down. When I then highlight the next row down (or really any other row), the undesired row is finally deleted and the 2nd and subsequent rows move up, which is what I expected to happen after the code is executed (except nothing should be highlighted). Obviously I am missing something that will cause the table to redisplay properly and the highlight to go away. Can someone point me in the right direction? TIA.
 
Paul Clapham
Sheriff
Posts: 21884
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would do those lines in this order: 3, 1, 4. Line 2 is unnecessary.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that got me a lot closer. I need to better understand why the order mattered. However, there is still a problem. After the row is removed, the table does not stay sorted. I am guessing I need to somehow figure out how it was sorted originally and then sort again the same way?
 
Paul Clapham
Sheriff
Posts: 21884
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a strange kind of sorting, where you remove one of the entries and now the rest aren't sorted. But perhaps you've used a TableRowSorter or something like that? If so then you have to be careful about choosing the internal or external row number to work with. (And no, I don't know which one is correct.)
 
Paul Clapham
Sheriff
Posts: 21884
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:I need to better understand why the order mattered.


You were already complaining that removing the selected row caused problems with your selection listener. So it seemed to me that unselecting that row before removing it would help with that problem. And calling revalidate() is pointless because the fireTableRowsDeleted() method is going to notify the JTable to redraw itself anyway.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you say it is a strange kind of sorting. The table can be sorted alphabetically on any particular column or not sorted at all. That seems pretty typical to me. In this test case it is sorted on column 2 (3rd column). After the delete it is no longer sorted on that column but seemingly a random sort. Yes, I am using TableRowSorter. I let it default everything except which columns are sortable (i.e. 0-2):
 
Paul Clapham
Sheriff
Posts: 21884
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, what I meant is that when you sort something in order: 1, 6, 7, 11, 23, 28 and then delete one of the entries, the rest of the entries are still sorted: 1, 6, 7, 23, 28. However since you're using a TableRowSorter, looks like all bets are off. I haven't tried modifying my sorted tables yet so if this were my problem I'd be going back to the tutorial (where I learned everything I know about TableRowSorter) to see what it had to say. I would expect there to be a solution which doesn't involve re-sorting the table, because in the general case your code isn't going to know what order the table used to be sorted in.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All that is needed is:



assuming of course that you are using the DefaultTablModel.

You should never invoke a fireXXX(..) method directly, it is the responsibility of the TableModel to invoke the appropriate method.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice and I think I am a little closer. The table stays sorted. However, the problem now is that it is removing the wrong row. Here is my current code:

I'm guessing that because I moved clearSelection to before the removeRow, the value of 'row' is no longer correct. So how do I determine what the row number to use? TIA.
 
Paul Clapham
Sheriff
Posts: 21884
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:But perhaps you've used a TableRowSorter or something like that? If so then you have to be careful about choosing the internal or external row number to work with. (And no, I don't know which one is correct.)


So perhaps this statement is still potentially relevant?
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:However, the problem now is that it is removing the wrong row.
You need to convert the row index from the view row to the model row. JTable has a method for that.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did that at one point but it didn't help. However, now that I have everything else working maybe it will work too. I just need to figure out if view is correct. Thanks.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. It was Model not View.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!