6

我使用JXTable的是来自 SwingX 组件的。如果我使用setSortable(boolean flag)方法,那么它将启用或禁用所有列的排序。

根据我的要求,我想禁用几列的排序并启用其他列的排序。

任何人都可以帮助实现此功能吗?


感谢您的回复。你能帮我setSorterClass(String sorterClassName)禁用一列的排序吗?你能给我任何代码示例吗?这对我很有帮助。

4

3 回答 3

4

SwingX 支持在 TableColumnExt 级别上按列排序的属性。它的默认值为 true,在创建列后禁用它

table.getColumnExt(myColumnIndex).setSortable(false)

或者在创建时,使用自定义的 ColumnFactory,比如

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 将负责将列属性同步到排序器,前提是它的类型为 SortController(这是默认设置)

于 2011-06-14T13:11:41.393 回答
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.

于 2011-05-21T09:57:12.267 回答
1

我认为你应该看看TableRowSorter API,看看 JXTable 是否支持它:

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

TableRowSorter 有一个 API 方法isSortable()

公共布尔 isSortable(int 列)

如果指定的列是可排序的,则返回 true;否则为假。

参数: column - 根据底层模型检查排序的列

返回:如果列是可排序的,则返回 true

于 2011-05-21T10:03:48.833 回答