aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes trying to get method working in TableModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "trying to get method working in TableModel" Watch "trying to get method working in TableModel" New topic
Author

trying to get method working in TableModel

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
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

Joined: Aug 22, 2010
Posts: 3611
    
  60

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

Joined: Jun 13, 2009
Posts: 2215
    
    7
Use a DefaultTableModel. It already supports this functionality.

You can always look at the source code of this class to see how it works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trying to get method working in TableModel