Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting JXTable with SwingX

I am using JXTable which is from SwingX components. If I use setSortable(boolean flag) method then it will enable or disable sorting for all columns.

As of my requirement I want to disable sorting for a few columns and enable sorting for other columns.

Can anyone help achieve this functionality?


Thanks for your reply. Can you help me with using setSorterClass(String sorterClassName) to disable sorting for one column? Could you give me any code examplex? It will be very helpful for me.

like image 917
user711466 Avatar asked May 21 '11 09:05

user711466


2 Answers

SwingX supports a per-column sortable property on the level of the TableColumnExt. It's default value is true, to disable it after column creation

table.getColumnExt(myColumnIndex).setSortable(false)

Or at creation time, use a custom ColumnFactory, like

ColumnFactory factory = new ColumnFactory() {

    @Override
    public void configureTableColumn(TableModel model, TableColumnExt column) {
        super.configureTableColumn(model, column);
        if (... your condition to disable sortable) {
            column.setSortable(false);
        } 
    }
} 
table.setColumnFactory(factory);
table.setModel(model);

JXTable will take care of synchronizing the column property to the sorter, provided it is of type SortController (which is the default)

like image 58
kleopatra Avatar answered Sep 28 '22 15:09

kleopatra


I think, at least according to what I have found on the net you can achieve it by setting setSorterClass(null) for that column.

As we can read on the cached web site, as swinglabs tutorial page appears to be down, I bet it has something to do with the recent mess on the java.net service. "JXTables have column sorting turned on by default. You can disable all column sorting using setSortingEnabled(boolean allowSort). You can also disable sorting on a single column by using setSorterClass(String sorterClassName) with a null sorter class name."

Personally, I think there is no point to block user from sorting on a selected table column. Anyway if a user wants to sort a column he/she should be able to do so, in the end I believe it is better to allow a user for more then less, of course when it goes to such details as what he/she can control in his/hers view.

like image 43
Boro Avatar answered Sep 28 '22 15:09

Boro