在不单击列标题的情况下对JTable的条目进行排序 [英] sort JTable's entries without clicking on column header
问题描述
我已经使用DefaultRowSorter和自定义定义的行排序器类在JTable上实现了行排序.单击列标题以对表进行排序,一切正常.
I've implemented row sorting on a JTable both using DefaultRowSorter and my custom defined row sorter class. All works fine clicking on the column header to sort the table.
但是,如果我想从应用程序代码内部调用排序操作(不单击列标题)怎么办?我必须调用哪种方法?
But what about if I want to invoke a sort operation from inside my application code ( without clicking on the column header). Which method do I have to call ?
我通过以下方式初始化表格的行排序器:
I initialize my tables row sorter this way:
public void buildRowSorter() {
TableRowSorter<MyModel> sorter = new TableRowSorter<MyModel>((MyModel)this.table.getModel());
try {
sorter.setComparator(0, new MyCustomComparator<Double>(sorter,0));
sorter.setComparator(1, new MyCustomComparator<String>(sorter,1));
} catch (ParseException e) {
e.printStackTrace();
}
this.table.setRowSorter(sorter);
}
现在,我想引用JTable(表),以将关联的行排序器检索到模型的特定列,并在其上调用排序操作.
Now I would like, having a reference to the JTable (table), to retrieve the associated row sorter to a particular column of my model, and invoke a sort operation on it.
推荐答案
您有一个自定义的比较器(为什么保留对分类器的引用?看起来很腥)自定义RowSorter.
as far as I can see you have a custom comparator (why does that keep a reference to the sorter? looks fishy) not a custom RowSorter.
更改排序的预期方法是在RowSorter上调用toggleSortOrder(column).要获得更细粒度的控制,您可能需要访问DefaultRowSorter,f.i.其setSortKeys方法.
The intended way to change sorting is to invoke toggleSortOrder(column) on RowSorter. For more fine-grained control you can need access to the DefaultRowSorter, f.i. its setSortKeys method.
这篇关于在不单击列标题的情况下对JTable的条目进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!