i am thinking of using the sampler TableSorter provided by Sun:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#"sorting"
I have a question however.
I am writing and app that recieves data from a feed and displays it in
table. So there is very little user interaction with the table.
Basically i have a thread that recieves the data, processes and
adds/updates/removes the data from the TableModel using some custom
methods (like addRow, getRow, etc..). When any either a cell or row
changes, i do fire the appropriate Table events so the JTable reflects
the changes.
Now if i were to add the TableSorter in between my Jtable and my
TableModel, can i continue to update my table model the same way i have
been doing or do i need to now add and call methods to TableSorter and
delegate to my TableModel methods (addRow, getRow, deleteRow, etc.).
In other words, will i now have to interface directly thru TableSorter
instead of My existing table model?
jonck - 07 Jun 2005 10:01 GMT
You are going to want to apply a technique called "model filtering"
here. Check out the following tutorial at
http://www-106.ibm.com/developerworks/java/library/j-filters/
They use ListModels in the examples, but everything mentioned in the
tutorial translates just fine to TableModels.
Kind regards, Jonck
Nigel Wade - 08 Jun 2005 12:18 GMT
> i am thinking of using the sampler TableSorter provided by Sun:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#"sorting"
> I have a question however.
> I am writing and app that recieves data from a feed and displays it in
[quoted text clipped - 10 lines]
> In other words, will i now have to interface directly thru TableSorter
> instead of My existing table model?
You use TableSorter in place of a TableModel (it extends DefaultTableModel),
and it presents a sorted view.
I think your easiest option would be to use TableSorter as the basis of your
new sorted model and merge your custom model into that. When I wanted a
sorted table that's pretty much what I did; I started with TableSorter and
then overrode the TableModel methods which I needed to.

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
farseer - 09 Jun 2005 18:04 GMT
After going ahead and just "doing it". it turns out that it works fine
without much additional code. in other words, i DID NOT have to use
TableSorter as the basis for my model, nor did i need to add code to
tablesorter to handle my specific cases. The reason makes sense
actually...with this "Model", i am updatint my Table model, but the
order never changes. Second, TableSorter keeps track of the view index
to the model index. Now when i fire my events after updating the
model, the table will call getValue. since tableSorter is the model it
is using, the JTable is actually calling tableSorter.getValue, and that
already is delegating to my table model. all works nicely it seems.
There was a problem with dealing with columns with custom renderers.
here i had to add a method of the table similar to
convertColumnIndexToModel. I called it convertRowIndexToModel. After
doing that and using that in my cellrenderer to access the correct
model data, all worked well
Richard Wheeldon - 08 Jun 2005 19:16 GMT
> Now if i were to add the TableSorter in between my Jtable and my
> TableModel, can i continue to update my table model the same way i have
> been doing
Yes. It's a one-line patch.
However:
1. You'll be updating one model and displaying a second (the sorter).
Be careful you don't update the sorted model directly or it'll get
very confusing.
2. You need to watch for the change in position when handling
selection events. This is another one-line patch.
3. If you insert several rows at a time in a single position, the
table sorter will need to insert several rows in different
positions. Rather that do this it refreshes the whole table.
This can be a severe performance hit. Keep to single row updates
and single cell updates and you'll be fine,
Richard
farseer - 09 Jun 2005 18:07 GMT
thanks for the advice Richard. after trying it, i have found all of
what you said to be true.