Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

jTable, sorting and selection

Thread view: 
ph0ng - 28 Feb 2005 20:51 GMT
hi,

i used a JTable component and used the TableSorter class from the sun
webpage to sort it (
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? are there other good table components available
that do have sorting and where the selection still works?

sorting looks to me like a major feature for a table component...

thx in advance
Kari Ikonen - 28 Feb 2005 21:44 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
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.