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 / November 2005

Tip: Looking for answers? Try searching our database.

TableSorter reselect selected rows

Thread view: 
joseph.hirn@gmail.com - 27 Oct 2005 16:54 GMT
Hello. I'm trying to use the Sun TableSorter and have an issue with it
deselecting the selected rows.

I know I need to save the selected rows and then reselect them using
the appropriate model to sorter index conversion but I can't seem to
discover exactly which events get thrown so I can override them in
TableSorter and how to feed it the Table object for getting and
reselecting the rows as well as scrolling to the selected row(s).

Does anybody have examples of an augmented TableSorter that maintains
the selected rows after sorting?
Roedy Green - 28 Oct 2005 08:37 GMT
>I know I need to save the selected rows and then reselect them using
>the appropriate model to sorter index conversion but I can't seem to
[quoted text clipped - 4 lines]
>Does anybody have examples of an augmented TableSorter that maintains
>the selected rows after sorting?

I wrote my own TableOrderer that kept tables in order as new elements
were added or removed.  

It also had a bulk sort.  

During a bulk sort there are not going to be any events fired.
Everything is in flux and it makes no sense to.

Only after the dust has settled would you issue an event to trigger a
full repaint.

If you handle this problem even in the most flat-footed way, the worst
that will happen is the entire table will repaint, then after that is
done your code will restore the selections, then you will fire events
for those rows, then they will be repainted.

Just write the code in a natural way and see how it behaves.  There is
a good chance it will be just fine.

Why is this?   Sun's sort may fire a repaint-the-entire-table event
however all that happens is the request is queued.  You are STILL
tying up the Swing thread. No painting can happen. You reset your
selections.  Let us say you now exit the method that triggered all
this giving Swing a chance to go back to dispatching events..

In a little while the repaint-everything  event will percolate to the
head of the queue.  The entire table will get repainted INCLUDING the
new selections.

What can go wrong? What if Sun's sort does NOT fire a
repaint-the-whole-table event.  Then it is up to you to do it when you
have everything ready to go.

Sun's sort might do some sort of paintImmediately. In which case your
later changes to the selections might not show since you did not fire
change events for them.

Understand everything I said here is based on complete ignorance of
how Sun's TableSorter works.  I am just explaining the basic
principles of how mine worked.  

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

joseph.hirn@gmail.com - 28 Oct 2005 15:16 GMT
Well I've already augmented about 15 table models using the TableSorter
so I would prefer to stick with it rather than custom code something to
intricate. What I seem to gather is that it is worthless to use the Sun
TableSorter.

Why does sorting a table involve abstract vector conversions? Won't
some swing zealot please come along and show me why Java is so much
easier than .Net?
Roedy Green - 28 Oct 2005 20:51 GMT
>Well I've already augmented about 15 table models using the TableSorter
>so I would prefer to stick with it rather than custom code something to
>intricate. What I seem to gather is that it is worthless to use the Sun
>TableSorter.

No you misunderstood. I am merely confessing my ignorance of how Sun's
TableSorter works.  I was simply explaining how table sorters in
general work.I was giving you instructions to deal with it giving
three plausible scenarios of how Sun's might work.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

jozeph78 - 01 Nov 2005 17:09 GMT
No I understood. I apologize for for being underappreciative of your
response.  I'm just a little frustrated because it should be a
moderately simple thing to do. But it seems in this case Sun is
perfectly happy providing you with wood, knife and compass and saying,
make your own damn wheel.
Roedy Green - 02 Nov 2005 03:58 GMT
> But it seems in this case Sun is
>perfectly happy providing you with wood, knife and compass and saying,
>make your own damn wheel.

Bingo!  that is exactly what JTable is.  Like the plans you send away
for to build a boat in the back of a magazine, or the "Bonzai kit"
that turns out to be a package of tree seeds. It is just a plan with
very little in the way of Sun's code.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

jozeph78 - 10 Nov 2005 17:02 GMT
Well for those of you who posted this question only receieve the "do it
yourself" answer, here is the code changes I made to the private
MouseHandler class. It gets the table from via
((TableHeader)MouseEvent.getSource()).getTable() to get the selcted
row's model index (note not selected rows), performs the sort, then
reselects the rows based on the model Index. Works well for me without
any changes to the Table using the sorter.

If anyone has any suggestions on how to tighten this up, please advise.

    private class MouseHandler extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
            int viewColumn;
            int column;
            int [] modelIndiciesOfSelectedRows;
            JTable table;
            JTableHeader h;
            TableColumnModel columnModel;

            h = (JTableHeader) e.getSource();
            table = h.getTable();
            // JH get selected rows to reset later before canceling and/or
setting sorting status
            modelIndiciesOfSelectedRows = getModelIndiciesOfSelectedRows(table);

            columnModel = h.getColumnModel();
            viewColumn = columnModel.getColumnIndexAtX(e.getX());
            column = columnModel.getColumn(viewColumn).getModelIndex();
            if (column != -1) {
                int status = getSortingStatus(column);
                if (!e.isControlDown()) {
                    cancelSorting();
                }
                status = (status <= 0) ? ASCENDING : DESCENDING;
                setSortingStatus(column, status);
            }

            reselectPreviouslySelectedRows(table,modelIndiciesOfSelectedRows);
        }

        private int [] getModelIndiciesOfSelectedRows(JTable table){
            int [] indicies = table.getSelectedRows();
            int [] modelIndicies = new int[indicies.length];
            for (int i = 0; i < indicies.length;i++){
                modelIndicies[i] =    modelIndex(indicies[i]);
            }
            return modelIndicies;
        }

        private void reselectPreviouslySelectedRows(JTable table, int []
selectedModelIndicies){
            if (selectedModelIndicies.length != 0){
                int selectedRow;
                for(int i = 0; i < selectedModelIndicies.length;i++){
                    selectedRow = getModelToView()[selectedModelIndicies[i]];
                    table.addRowSelectionInterval(selectedRow,selectedRow);
                }
                Rectangle rect = table.getCellRect(table.getSelectedRow(),0,true);
                table.scrollRectToVisible(rect);
            }
        }
    }


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.