JTable - sorting all columns except first

Discussion in 'Java' started by Lukasz, Aug 23, 2006.

  1. Lukasz

    Lukasz Guest

    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.
    Lukasz, Aug 23, 2006
    #1
    1. Advertising

  2. 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
    Michael Rauscher, Aug 24, 2006
    #2
    1. Advertising

  3. Lukasz

    Lukasz Guest

    > 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);
    }
    Lukasz, Aug 24, 2006
    #3
  4. 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
    Michael Rauscher, Aug 24, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cG11ZA==?=
    Replies:
    2
    Views:
    989
    =?Utf-8?B?cG11ZA==?=
    Jan 10, 2005
  2. John Salerno
    Replies:
    20
    Views:
    848
    John Salerno
    Aug 11, 2006
  3. Fabio Z Tessitore

    who is simpler? try/except/else or try/except

    Fabio Z Tessitore, Aug 12, 2007, in forum: Python
    Replies:
    5
    Views:
    372
  4. David House

    try -> except -> else -> except?

    David House, Jul 6, 2009, in forum: Python
    Replies:
    2
    Views:
    334
    Bruno Desthuilliers
    Jul 6, 2009
  5. Peng Yu
    Replies:
    1
    Views:
    515
    Steven D'Aprano
    Nov 18, 2009
Loading...

Share This Page