JTable - sorting all columns except first

L

Lukasz

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/TableSorterDemo.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.
 
M

Michael Rauscher

Lukasz said:
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
 
L

Lukasz

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

Michael Rauscher

Lukasz said:
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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top