After adding a TableRowSorter to a table and its corresponding model any corresponding adds specifically at firetabletablerowsinserted cause exceptions. It is clear from
testing that the GetRowCount() is returning a value past the models range. However it does not make sense to me how to continue to add values to the table after a sorter or filter has been added?
As an example, I set the row filter before adding anything to the table then add a value to the table with the following calls in my model:
The rowcount is of size 1 and the exception is thrown:
java.lang.IndexOutOfBoundsException: Invalid range at javax.swing.DefaultRowSorter.checkAgainstModel(Unknown Source) at javax.swing.DefaultRowSorter.rowsInserted(Unknown Source) at com.gui.model
If I do the same steps without first adding the sorter everything is fine. I assumed that possibly I needed to notify the model that the sorter may have made changes and tried the following but still returns an exception:
So, to summarize I have tried to call the this.fireTableRowsInserted(this.getRowCount(), this.getRowCount()); with both the sorter rowcount and that fails. So, I also tried to check in the update when adding to the table to see if a sorter exists. If the sorter exists I try to update the sorter itself. This does work like the following example. Problem I have with this is two fold.
1. Its calling update on the entire table. Once the table gets large its updating sort for the entire table. When only one row has been added. It really should only need to call sort on the new row. But any time I get specific like sorter.rowsInserted(firstRow, endRow) I get invalid range exceptions.
2. You would think that by updating the model you would niot need to worry about handling another model. I was hoping it would delegate sorting once the model its sorting is updated?
If anyone could try to clarify how best to handle this I would be very grateful. I will post the entire Update method below this method essentialy is the update inside my tablemodel class. All observers of the model add data to the table from this method by using Observer
pattern.