Following Situation: I have a J(X)Table with RowHeader (As guidline I used one of Rob Camicks great Examples). All worked as expected.
By requirement the data I receive from server already contains a tablerownumber, which I have to show in the rowheader and the data should be filterable. So I extended the example, and I added a filter. When I filtered the view I saw gaps in my row numbers (for example: 1, 3, 6,..), which is the desired effect.
To be able to filter and sort the table by my own tablerow, I added a TableRowSorter
. And here I started to get confused. The Example uses the same TableModel and SelectionModel for mainTable and rowHeaderTable:
setModel( main.getModel() );
setSelectionModel( main.getSelectionModel() );
This is great, since I don’t have to synchronize them. But concerning TableRowSorter
I suddenly wasn’t sure, if I also can or even have to use the same TableRowSorter
-Instance or if I have to create a TableRowSorter
for each table. First I added the same to both Tables, since this seemed practically, but then I got IndexOutOfBound-Exceptions
in many cases. After some digging I found out that this is because the TableRowSorter
gets updated twice at each TableModelEvent
, because each table (RowHeader and MainTable) notifies the TableRowSorter
about tablechanges on its own.
Now I am not sure which the right way to go is. Following solutions came into my mind: Should I add a second TableRowSorter (one for each table) and synchronize these, or should I wrap the TableModel within the RowHeaderTable and let it not fireing any Events? Or maybe I should create my own kind of RowHeaderTable which doesn’t notify Sorters about changes at all?
Here's a quick (beware: not formally tested! the usage example works fine, though) implementation of a wrapping RowSorter.
It's client's responsibility to keep it in synch with the rowSorter used in the main table
Usage example (in terms of SwingX test infrastructure and with SwingX sortController/table):
The RowSorterWrapper: