如何使用 JTable 的行排序器恢复原始行顺序?
我已使用 setAutoCreateRowSorter 方法在 JTable 中启用排序。鼠标单击列标题将在升序和降序之间切换,但我想在升序、降序和原始(未排序)行顺序之间切换。有任何提示如何实现这一目标吗?
I've enabled sorting in JTable with setAutoCreateRowSorter method. Mouse clicks on the column headers will switch between ascending and descending order, but I would like to switch it between ascending, descending and the original (unsorted) row order. Any hints how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
鼠标单击到更改排序状态的映射是在 BasicTableHeaderUI 中实现的,并且仅通过 RowSorter 的toggleSortOrder(columnIndex) 发生。它的默认行为是切换
UNSORTED -->升序 -->降序 -->升序——
也就是说,无法回到未排序状态。如果要求只是简单地将圈子扩大为:
UNSORTED -->升序 -->降序 -- 未排序 --> ASCENDING ——
方法是子类化 TableRowSorter 并相应地重写其toggleSortOrder
注意:这有点简化,因为它不考虑 n 元排序列,请参阅 SwingX DefaultSortController 了解完整版本。或者使用SwingX开始,它的JXTable有api来配置排序周期,就像
Cheers 一样
珍妮特
The mapping of a mouse click to changing the sort state is implemented in BasicTableHeaderUI and happens exclusively via the RowSorter's toggleSortOrder(columnIndex). It's default behaviour is to switch
UNSORTED --> ASCENDING --> DESCENDING --> ASCENDING --
that is, no way back into the UNSORTED. If the requirement is to simply widen the circle into:
UNSORTED --> ASCENDING --> DESCENDING -- UNSORTED --> ASCENDING --
the way to go is to subclass TableRowSorter and override its toggleSortOrder accordingly
Note: this is a bit simplified as it doesn't account for n-ary sorted columns, see SwingX DefaultSortController for a full version. Or use SwingX to start with, its JXTable has api to configure the sorting cycle like
Cheers
Jeanette
此代码实现循环 ASCENDING->DESCENDING->UNSORTED->... 而不重写 TableRowSorter:
This code implements cycle ASCENDING->DESCENDING->UNSORTED->... without overriding TableRowSorter:
如果您使用的是
DefaultRowSorter
:“空的sortKeys
列表表示视图应该是未排序的,与模型相同。”附录:请注意
setSortKeys()
。If you're using the
DefaultRowSorter
: "An emptysortKeys
list indicates that the view should [be] unsorted, the same as the model."Addendum: Note that "
null
is a shorthand for specifying an empty list" insetSortKeys()
.科内尔的答案对我来说非常有用(谢谢!)
我做了这些更改,其他人可能会觉得有用:
每当列更改时,重新初始化排序顺序。这种方式的用户体验明显更加直观,因为您可以连续单击不同的列并每次都获得升序。
Kornell's answer is working great for me (thanks!)
I made these changes which others may find helpful:
Whenever column changes, reinitialize sort order. User experience is significantly more intuitive this way, as you can successively click different columns and get ascending order each time.
由于内置排序机制仅提供升序或降序,因此如果您在 JTable 附近的某个位置提供外部重新加载 JButton,则可以返回到原始状态,该按钮将相同的模型设置回您所显示的模型在进行任何排序活动之前,第一次查看该表。
Since the built-in sorting mechanism gives you ascending or descending order only, you could return to the original state if you provide an external reload-JButton, somewhere near your JTable, that just sets the same model back to what you had as you displayed the Table the first time, before any sorting activity took place.
如果您正在寻找执行此操作的 UI 方式 - 按住 Shift 键并单击列标题即可执行此操作。这是内置的 JTable 功能
If you looking for UI way of doing it - shift-click on column header will do it. This is built-in JTable functionality