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 / General / August 2006

Tip: Looking for answers? Try searching our database.

JTable - sorting all columns except first

Thread view: 
Lukasz - 23 Aug 2006 12:53 GMT
Hi,

I'm using TableSorter from page:

http://java.sun.com/docs/books/tutorial/uiswing/components/examples/TableSorter.java

and a JTable which works with this sorter:

http://java.sun.com/docs/books/tutorial/uiswing/components/examples/TableSorterD
emo.java


I want to make the sorter work that way, that the first column will
always stay as it is - unsorted, even if one of the other columns is
sorted.

In mouseHandler of TableSorter class I replaced:
if (column != -1)

with:
if ((column != -1) && (column != 0))

Now, if I click the header of the first column, it won't be sorted. But
when any other header is pushed, the first columns is beeing sorted.

Glad for any help.
Michael Rauscher - 24 Aug 2006 09:08 GMT
Lukasz schrieb:
> Hi,
>
> I'm using TableSorter from page:
...

> I want to make the sorter work that way, that the first column will
> always stay as it is - unsorted, even if one of the other columns is
> sorted.

Have a look at how TableSorter works. It maps "view indices" to "model
indices" and vice versa. Therefore sorting the table means just to
update these indices. It is important to recognize this because it means
that the underlying model won't be changed.

The next thing to think about is how JTable gets the values to present
and how JTable applies changed values. This is done via getValueAt and
setValueAt.

Now, have a look at these two methods, you'll find the expression
    modelIndex(row)
there. This method takes a view index and returns a model index.

So, all you need to do is to change this mapping in these two methods, e. g.

public Object getValueAt( int row, int col ) {
    int modelRow = ( col > 0 ? modelIndex(row) : row );
    return tableModel.getValueAt( modelRow, col );
}

Haven't tried it but seems to be OK while looking at TableSorter.java.

Bye
Michael
Lukasz - 24 Aug 2006 09:53 GMT
> So, all you need to do is to change this mapping in these two methods, e. g.
>
[quoted text clipped - 4 lines]
>
> Haven't tried it but seems to be OK while looking at TableSorter.java.

Thanks Michael, that works. Now these two methods look like this:

int modelRow = 0;

   public Object getValueAt(int row, int column) {
       //return tableModel.getValueAt(modelIndex(row), column);
       modelRow = ( column > 0 ? modelIndex(row) : row );
        return tableModel.getValueAt( modelRow, column );
   }

   public void setValueAt(Object aValue, int row, int column) {
       tableModel.setValueAt(aValue, modelRow, column);
   }
Michael Rauscher - 24 Aug 2006 11:57 GMT
Lukasz schrieb:
> Thanks Michael, that works. Now these two methods look like this:
>
[quoted text clipped - 7 lines]
>
>     public void setValueAt(Object aValue, int row, int column) {
        modelRow = ( column > 0 ? modelIndex(row) : row );
>         tableModel.setValueAt(aValue, modelRow, column);
>     }

Otherwise you'll run into trouble.

Bye
Michael


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



©2009 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.