• Post Reply Bookmark Topic Watch Topic
  • New Topic

delete row from Jtable using AbstractTableModel  RSS feed

 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I have the following


Now after I click the delete button, that deleted record is still displayed in the table view but as "java.lang.Object@..." for the field. I want to completely remove the just deleted row from the view.

Also I noticed if there are say 20 records, I delete the 5th record. The 20th record disappears with the 5th record display as "java.lang.Object...". If I managed to remove the 5th record, would I able to see the 20th record again?

Oh the removeRow method is practically the same as the DefaultTableModel implementation. But it doesn't look right.
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Hi all, I have the following


Now after I click the delete button, that deleted record is still displayed in the table view but as "java.lang.Object@..." for the field. I want to completely remove the just deleted row from the view.

Also I noticed if there are say 20 records, I delete the 5th record. The 20th record disappears with the 5th record display as "java.lang.Object...". If I managed to remove the 5th record, would I able to see the 20th record again?

Oh the removeRow method is practically the same as the DefaultTableModel implementation. But it doesn't look right.


MyTableModel.removeRow() needs to call the fireTableRowsDeleted() method. What this does is notify the listeners (i.e. the JTable) so they can update themselves.

Btw, it's hard to tell from what you've shown us, but it appears that you are storing all the data twice: once in MyTableModel's map and once in the data object. Wouldn't it be easier to just store it once? For instance, MyTableModel could simply refer to data.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian, you are right about storing the data twice, so I removed the map object. Now in my deleteRow() method, I did call the fireTableRowsDeleted() method. But doesn't seem to work. Once I remove the map, I use a 2D array to populate the table's data. Now the deleteRow() method looks like this:



Now once I delete the row the table view becomes blank for that particular row (from line 11 above). But actually I really want to remove that from the 2D array. Since 2D array size is fixed, I could repopulate the 2D array from scratch by calling my table model's constructor which would get the non deleted row record number from the data object. This ends up the very last record missing once I delete row.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!