• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting and adding a row in table

 
Lang Miao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can do sorting and adding row separately with no problem. But when I combine the two features, I got a problem.

Here's how the whole thing initialized.


The sorting feature is fine, but the adding row is not. Here's how I add row.


The following is the exception thrown when adding a row


Can the exception be resolved? Or do I have to extends DefaultTableModel (instead of AbstractTableModel) because it has method addRow()?

Thanks for your time.
 
Klaas van Gelder
Ranch Hand
Posts: 111
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on s sortable JTable and encountered the same problem.
Most simplemexamples use a TableSorter (implementing TableModel) which hold a Row[] array to keep the mapping between the sorted rows and the rows in the model. This Row[] array is created in the constuctor of the TableSorter holding just as many elements as the INITIAL number of rows in the tablemodel.
But when adding a new row, the returnvalue of th getRowCount() method of the TableModel increases by 1, but the Row[] array in the TableSorter is not updated. Therefore an ArrayIndexOutOfBoundsException is thrown.
If you use the TableSorter class in the JDK example handles this problem by refilling the Row[] array in its getViewToModel method.
I hope this is of help for you....
Regards, Klaas
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!