JTable +排序特定字段 [英] JTable + Sorting specific field

查看:261
本文介绍了JTable +排序特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable并添加了排序。现在,JTable有5列,第2列在日期字段中转换为DD / MM / YYYY并显示在单元格中的JTextField中。

I have a JTable and have added sorting. Now the JTable has 5 columns and the 2nd column in a date field converted to DD/MM/YYYY and displayed in a JTextField in the cell.

当我对它进行排序时作为字符串和我日期混淆,如何更改该特定列的排序行为?

When I sort it sorts as string and I the dates get mixed up, how do I change the behaviour of sorting for that particular column?

例如。按照ASC顺序排序后,我得到了这个:

eg. after sorting in ASC order, I get this:

01/02/2012
01/03/2011
01/04/2011
01/05/2011
01/06/2011
01/07/2011
01/08/2011
01/09/2011
01/10/2011
01/12/2011

其中是错的,我应该得到的结果如

Which is wrong, and I should be getting the result like

01/03/2011
01/04/2011
01/05/2011
01/06/2011
01/07/2011
01/08/2011
01/09/2011
01/10/2011
01/12/2011
01/02/2012

我的代码现在看起来像这样排序

My code now looks like this for sorting

List<SortKey> sortKeys = new ArrayList<SortKey>();
sortKeys.add(new SortKey(2, SortOrder.ASCENDING));
table.getRowSorter().setSortKeys(sortKeys);

我应该仅针对该特定列更改什么?

What should I change for that specific column only?

推荐答案

您需要实现比较器,将日期字符串视为日期相当简单 String 看看这里

You need to implement comparator that treats date string as Date rather simple String have a look here

这篇关于JTable +排序特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆