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 sor开发者_如何转开发ting 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.
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)
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.
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
精彩评论