简体   繁体   English

添加新行后如何自动对JTable排序?

[英]How to automatically sort JTable after adding new row?

在向JTable添加新行时如何在JTable中对行进行升序或降序排序?

Depending on your needs whether or not you need to sort on multiple columns, you can add a TableModelListener to your table model, to execute custom behaviour when the table model changes. 根据您的需要,是否需要对多列进行排序,可以将TableModelListener添加到表模型中,以在表模型更改时执行自定义行为。 Just implement the 只需实施

public void tableChanged(TableModelEvent e)

method to capture the event and sort the required collumn. 捕获事件并对所需列进行排序的方法。 One way of doing this is to map work with an index map which refers to the data of the column you want to sort and resort the index map according to the column that you want to sort. 一种实现方法是使用索引图进行映射,索引图引用要排序的列的数据,并根据要排序的列对索引图进行重新排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM