Sorting and Table model

F

farseer

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?
 
N

Nigel Wade

farseer said:
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?

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.
 
R

Richard Wheeldon

farseer said:
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
 
F

farseer

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
 
F

farseer

thanks for the advice Richard. after trying it, i have found all of
what you said to be true.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top