Sorting JXTable with SwingX
Asked Answered
B

3

6

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.

Buttonhole answered 21/5, 2011 at 9:21 Comment(0)
A
4

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)

Allegraallegretto answered 14/6, 2011 at 13:11 Comment(1)
+1 Very well explained and presented how to do it. Thanks for correcting/updating me. :)Sutlej
S
2

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.

Sutlej answered 21/5, 2011 at 9:57 Comment(3)
agreed with "there is no point to block user from sorting on a selected table column" +1Wassyngton
that's outdated (or more precise: no longer existing as of 1.6 ;-) api.Allegraallegretto
@Allegraallegretto interestning. I was not aware of this fact. And I couldn't find a better answer manily because of the difficulty of using the web page being the result of java.net migration (more like devastation to me :)) of projects. What would you suggest instead, I wonder?Sutlej
D
1

I think you should have a look at TableRowSorter API and see whether JXTable supports it like:

TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
table.setRowSorter(new TableRowSorter(myModel));

TableRowSorter has an API method isSortable():

public boolean isSortable(int column)

Returns true if the specified column is sortable; otherwise, false.

Parameters: column - the column to check sorting for, in terms of the underlying model

Returns: true if the column is sortable

Dairyman answered 21/5, 2011 at 10:3 Comment(2)
Thanks for your reply. Can you help to know how can I use setSorterClass(String sorterClassName) for disable sorting for one column. Can you give any code example. It will be very helpful for me.Buttonhole
-1 for spreading a not-swingx'ish approach. There's no obligation to answer if you don't know SwingX/JXTable ;-)Allegraallegretto

© 2022 - 2024 — McMap. All rights reserved.