简体   繁体   English

Swing JTable按日期排序

[英]Swing JTable sort by Date

I would like to sort a Jtable by the date in one column, but only display the date in the format dd-MM-yyyy. 我想在一列中按日期对Jtable进行排序,但只以dd-MM-yyyy格式显示日期。 So all entries are the same and only differ in the seconds which are not visible. 所以所有条目都是相同的,只是在不可见的秒数上有所不同。

I have a TableModel which gets the data with the following method: 我有一个TableModel,它使用以下方法获取数据:

@Override
public Object getValueAt(int row, int col) {

    Object[][] tableData = new Object[rowDataMap.keySet().size()][1];
    int index = 0;
    for (Long key : pane.nqm_messages.keySet())
    {
        Date date = rowDataMap.get(key);

        SimpleDateFormat form = new SimpleDateFormat("dd-MM-yyyy"); 
        String outputDate = form.format(date);

        tableData[index][0] = outputDate;

        index++;
    }
    return tableData[row][col];
}

And here is my TableRowSorter where I want to sort the rows: 这是我的TableRowSorter,我想对行进行排序:

TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
List<SortKey> keys = new ArrayList<SortKey>();

SortKey sortKey;
sorter.setComparator( 0, new Comparator<String>() {  
    @Override
    public int compare(String s1, String s2)  {
        //Sort dd-MM-yyyy 
    }
});

If I do it like that I cant sort it because the strings are obv. 如果我这样做,我无法对它进行排序,因为字符串是obv。 all the same. 全都一样。 When I directly use the date object like that 当我直接使用这样的日期对象时

tableData[index][0] = date tableData [index] [0] =日期

I do not know how to display it in the correct format but the sorting can be done. 我不知道如何以正确的格式显示它,但可以完成排序。

How can I achieve both? 我如何实现这两个目标?

Don't convert the Date objects to String . 不要将Date对象转换为String Instead, use them directly within the model. 而是直接在模型中使用它们。 It's not the models responsibility to suggest formatting, instead use a TableCellRenderer . 建议格式化不是模型的责任,而是使用TableCellRenderer

Allow getValueAt to return the Date object. 允许getValueAt返回Date对象。

Modify the table model and override the getColumnClass method and return the approiate class for the hiven columns (like Date.class ). 修改表模式并重写getColumnClass方法,并返回approiate类的海闻的列(如Date.class )。

The table, will by default, format the Date objects for you. 默认情况下,该表将为您设置Date对象的格式。

You can supply your own TableCellRenderer if the default one is not to your liking. 如果默认的TableCellRenderer不符合您的喜好,您可以提供自己的TableCellRenderer

See How to use tables for more details, pay special attention to Using Custom Renderers 有关更多详细信息,请参阅如何使用表 ,请特别注意使用自定义渲染器

Whenever the display and inner logic differs you should consider a TableModel and a CellRenderer . 每当displayinner逻辑不同时,您应该考虑使用TableModelCellRenderer Why don't you add a CellRenderer on the date column? 为什么不在日期列上添加CellRenderer? The cell renderer renders the date object using the SimpleDateFormat, however the inner values remain as Date objects. 单元格渲染器使用SimpleDateFormat渲染日期对象,但内部值仍为Date对象。 That way the sorting should work correctly, because the sorter works on the inner values. 这样排序应该正常工作,因为分拣机处理内部值。

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

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