Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JTable - sorting all columns except first

Reply
Thread Tools

JTable - sorting all columns except first

 
 
Lukasz
Guest
Posts: n/a
 
      08-23-2006
Hi,

I'm using TableSorter from page:

http://java.sun.com/docs/books/tutor...bleSorter.java

and a JTable which works with this sorter:

http://java.sun.com/docs/books/tutor...orterDemo.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.

 
Reply With Quote
 
 
 
 
Michael Rauscher
Guest
Posts: n/a
 
      08-24-2006
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
 
Reply With Quote
 
 
 
 
Lukasz
Guest
Posts: n/a
 
      08-24-2006
> 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.


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);
}

 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      08-24-2006
Lukasz schrieb:
> 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) {

modelRow = ( column > 0 ? modelIndex(row) : row );
> tableModel.setValueAt(aValue, modelRow, column);
> }


Otherwise you'll run into trouble.

Bye
Michael
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between 'except IOError as e:' and 'except Peng Yu Python 1 11-18-2009 02:38 AM
try -> except -> else -> except? David House Python 2 07-06-2009 05:48 PM
who is simpler? try/except/else or try/except Fabio Z Tessitore Python 5 08-13-2007 12:52 AM
converting a nested try/except statement into try/except/else John Salerno Python 20 08-11-2006 02:48 PM
Exporting data from (All columns in data grid EXCEPT THE 1st) to E =?Utf-8?B?cG11ZA==?= ASP .Net 2 01-10-2005 07:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57