>http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#sorting
> the sorting works very good. but the TableSoter-layer between my JTable
> component and the TableModel distroys the selection indexes. so i can't
> detect which row the user selected. this seems to be a known issue. But
> what can i do about it?
Retaining selection without having unique IDs for each row is tricky
business. What you can try to do is to save selection before sort and
restore selection after sort.
1) Let table get notifications about row delete/insert
* i.e. standard JTable selection behaviour
2) Save selection
* i.e. resolve sorted selection rows into actual model's rows
3) do sorting
4) Restore selection
* i.e. try to map model rows back to sorted rows
* If selected rows haven't really changed, then don't touch selection.
5) Notify table that contents has changed
For save/restore you may establish some listener.
If you can use some column values as unique IDs then selection restore can
be more robust.
--
KI
Andrew McDonagh - 28 Feb 2005 22:15 GMT
>>http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#sorting
>>the sorting works very good. but the TableSoter-layer between my JTable
[quoted text clipped - 23 lines]
> --
> KI
What we have done to achieve good table sorting, is to use inheritance
rather than delegation (as in the TableModel decorator example).
We used the same design that JTable uses for its ColumnModel, but for
row - a RowModel. This allows rows to be added, updated, removed,
selected and unselected, whilst maintaining sort order.
It does require a few conversion methods like JTables existing
convertColumnIndexToModel() and convertColumnIndexToView()
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#convertColumnInd
exToModel(int)
so our DerivedJtable class has convertRowIndexToModel() &
convertRowIndexToView() methods.
It works very well.
Andrew