使用构建是否有人知道,以显示在JTable的头的图标排序的好方法,在不排序功能的使用构建?显示JTable中头图标排序没有排序机制
的排序是由表模型(实际上是一个数据库),而不是根据JTable本身来完成。这就是为什么图标的自动显示不起作用。也许一个可以插入一个虚拟RowSorter的,什么也不做,反而使得分类图标出现在哪里?
我发现了一个更好的解决方案
我只是写我自己的RowSorter的,这样排序不会有什么影响,但排序请求重定向模型代替。这样的排序顺序是由外观显示和感觉本身。一些伪:
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.swing.RowSorter;
import xyz.SortableTableModel;
public class MyRowSorter<M extends SortableTableModel> extends RowSorter<M> {
private M tableModel;
private List<? extends SortKey> sortKeys = new LinkedList<>();
public MyRowSorter(M tableModel) {
this.tableModel = tableModel;
}
@Override
public M getModel() {
return tableModel;
}
@Override
public void toggleSortOrder(int column) {
// redirecting sort request to model and modification of sortKeys
List<? extends SortKey> newSortKeys = ...;
setSortKeys(newSortKeys);
}
@Override
public int convertRowIndexToModel(int index) {
return index; // will always be the same
}
@Override
public int convertRowIndexToView(int index) {
return index; // will always be the same
}
@Override
public void setSortKeys(List<? extends SortKey> keys) {
if (keys == null) {
sortKeys = Collections.EMPTY_LIST;
} else {
sortKeys = Collections.unmodifiableList(keys);
}
fireSortOrderChanged();
}
@Override
public List<? extends SortKey> getSortKeys() {
return sortKeys;
}
@Override
public int getViewRowCount() {
return tableModel.getRowCount();
}
@Override
public int getModelRowCount() {
return tableModel.getRowCount();
}
// no need for any implementation
@Override public void modelStructureChanged() { }
@Override public void allRowsChanged() { }
@Override public void rowsInserted(int firstRow, int endRow) { }
@Override public void rowsDeleted(int firstRow, int endRow) { }
@Override public void rowsUpdated(int firstRow, int endRow) { }
@Override public void rowsUpdated(int firstRow, int endRow, int column) { }
}
您可以尝试使用图标行为编写自定义TableHeader渲染器。 – alex2410
我试过了。但是这个解决方案的问题是,标题渲染器应该使用系统外观。自定义渲染器显然不会使用外观和感觉(除了看起来像标签或其他东西)。 – user3190008
查看我的答案,它提供了默认的L&F和头部装饰解决方案。 – alex2410