【问题标题】:How to sort date and time in one column in a JTable?如何在 JTable 的一列中对日期和时间进行排序?
【发布时间】:2020-02-25 17:45:14
【问题描述】:

我想对 LocalDateTime 之类的内容进行排序。

在我的JTable 中,我的值如下:

30.10.2019 09:35:34
12.09.2019 07:47:55
12.09.2019 22:49:55
24.09.2019 04:45:39

现在我希望它们被正确排序,例如:

12.09.2019 07:47:55
12.09.2019 22:49:55
24.09.2019 04:45:39
30.10.2019 09:35:34

我尝试自定义我的 TableModel,但没有成功

@Override
public Class<?> getColumnClass(final int columnIndex)
{
    if (getColumnName(columnIndex).equals("Datecolumn")) return LocalDateTime.class;    
    try
    {
        return getValueAt(0, columnIndex).getClass();
    }
    catch (final Exception e)
    {
        return new String().getClass();
    }

}

Date.classnew Date().getClass() 也不起作用。

也许你对我有一些建议?

更新:

我终于做到了,所以这是解决方案:

table.setAutoCreateRowSorter(true);
          TableRowSorter<TableModel> sorter = new TableRowSorter<>(table.getModel());
          table.setRowSorter(sorter);
          int columnIndexToSort = table.getColumnModel().getColumnIndex("Datecolumn");
          sorter.setComparator(columnIndexToSort, new Comparator<String>()
          {
             @Override
             public int compare(String o1, String o2)
             {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.uuuu HH:mm:ss");
                LocalDateTime ldt1, ldt2;
                ldt1 = LocalDateTime.parse(o1, formatter);
                ldt2 = LocalDateTime.parse(o2, formatter);
                return ldt1.compareTo(ldt2);
             }
          });

我知道还有很多其他方法可以解决这个问题,但这是第一个适合我的方法。

这可能会对您有所帮助:
https://www.codejava.net/java-se/swing/6-techniques-for-sorting-jtable-you-should-know

有什么改进吗?

【问题讨论】:

  • 排序一般可以通过TableModel来实现。您是否已调试并确信列名与“Datecolumn”匹配?
  • 您的专栏需要可编辑吗?
  • tomgeraghty3:当我调用“getColumnClass”方法时,我得到了正确的类,但排序不起作用 Ole V.V:不,它没有
  • 如需更好的帮助,请edit 添加minimal reproducible exampleShort, Self Contained, Correct Example。对一些数据进行硬编码。

标签: java swing sorting datetime jtable


【解决方案1】:

三个步骤:

  1. 将日期时间对象放入数据和/或表模型中,而不是字符串。
  2. 使用自定义渲染器来格式化单元格中的日期时间。
  3. 正如 tomgeraghty3 所说,告诉表自动生成行排序器。

将日期时间对象放入您的数据中

可能Instant 对象确实最适合保存您的数据,但您可以自己决定更好。为了使这个示例简单,并且仅出于此目的,我使用了LocalDateTime 对象。

    Object[] columnNames = { "Date and time" };
    Object[][] data = {
            { LocalDateTime.of(2019, Month.OCTOBER, 30, 9, 27) },
            { LocalDateTime.of(2019, Month.SEPTEMBER, 26, 11, 25, 9) },
            { LocalDateTime.of(2019, Month.OCTOBER, 4, 15, 54, 25) },
            { LocalDateTime.of(2019, Month.OCTOBER, 13, 9, 2, 29) }
    };
    table = new JTable(data, columnNames);

使用自定义渲染器

    table.setDefaultRenderer(Object.class, new MyDateTimeRenderer());

据我了解,应该可以将 LocalDateTime.class 指定为仅用于 LocalDateTime 对象的渲染器,但这对我不起作用,我不知道为什么。

我的MyDateTimeRenderer 类如下所示:

public class MyDateTimeRenderer implements TableCellRenderer {

    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.uuuu HH;mm;ss");

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        String formattedDateTime = ((LocalDateTime) value).format(formatter);
        Component c = renderer.getTableCellRendererComponent(table,
                formattedDateTime, isSelected, hasFocus, row, column);
        return c;
    }

}

告诉表格自动生成一个行排序器

    table.setAutoCreateRowSorter(true);

您可以在致电setDefaultRenderer() 之前或之后将此声明。我觉得放在前面更自然。

结果

现在表格在排序前是这样的:

单击列标题“日期和时间”进行排序。结果:

再次单击以反向排序,降序:

链接

Using Custom Renderer with JTables

【讨论】:

  • 谢谢,这是一个很好的答案,但是当我尝试您的解决方案时,我发现我的 DateTime 默认存储为字符串。但这没问题。我只是解析(而不是格式化)我的字符串。
【解决方案2】:

确保已设置行排序器:

table.setAutoCreateRowSorter(true);

【讨论】:

    猜你喜欢
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    相关资源
    最近更新 更多