• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trying to get method working in TableModel

 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I've added a new method to my TableModel for JTable which doesn't seem to work as I expected. Note the following code:



What's interesting in this class is that the removeRow(int row) method works fine. Removing a single row with this method does the job. All I'm really doing is replacing the data[][] array with a new instance of Object[][] data. The removeAllData() method does the same thing, only it replaces the data[][] with a new instance of Object[0][0] with no elements. At runtime, if the JTable is populated and I call removeAllData() on the TableModel nothing changes in JTable. The old data is still displaying. What am I dong wrong?

Alan
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just guessing: try to replace your 17th line with
Arguments to fireTableRowsDeleted are inclusive, you're therefore advertising deletion of a row which is actually beyond the last row.

You could also call fireTableDataChanged instead, it tells the table all data (but not the structure) of a table has changed.

A related bug seems to be there: when removing a single row, you don't resize the array, and you use its length to count rows, eg. in removeAllData). I strongly suggest to keep your data in an ArrayList instead. You'll save yourself a lot of hassle with maintaining the array "by hand".

Though it could easily be a list of arrays, I'd create a class to hold data for one row and use a list of instances of this class, but that's a different story.
 
Rob Camick
Ranch Hand
Posts: 2599
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a DefaultTableModel. It already supports this functionality.

You can always look at the source code of this class to see how it works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic