JXTable Sort with SwingX

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

At my request, I want to disable sorting for multiple columns and enable sorting for other columns.

Can someone help to achieve this functionality?


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

+6
source share
3 answers

SwingX supports the sort property per column at the TableColumnExt level. The default value is true to disable it after creating the column.

 table.getColumnExt(myColumnIndex).setSortable(false) 

Or at creation time, use a custom ColumnFactory, e.g.

 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 with the sorter if it is of type SortController (which is the default)

+4
source

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

How can we read the cached website because the swinglabs tutorial page does not seem to work, I'm sure it has something to do with the recent java.net mess mess. "JXTables enables default column sorting. You can disable all column sorting using setSortingEnabled (boolean allowSort). You can also disable single column sorting using setSorterClass (String sorterClassName) with the class name of the null sorter."

Personally, I think that it makes no sense to block user sorting in the selected column of the table. In any case, if the user wants to sort the column, he / she should do it, in the end, I believe that it is better to allow the user more than less, of course, when it comes to details such as what he / she can control in his / her gaze.

+2
source

I think you should take a look at the TableRowSorter API and see if JXTable supports:

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

TableRowSorter has an isSortable () API method:

public boolean isSortable (int column)

Returns true if the specified column is sorted; otherwise false.

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

Returns: true if the column is sorted

+1
source

Source: https://habr.com/ru/post/888694/


All Articles