简体   繁体   English

JTable + 排序特定字段

[英]JTable + Sorting specific field

I have a JTable and have added sorting.我有一个 JTable 并添加了排序。 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.现在 JTable 有 5 列,日期字段中的第二列转换为 DD/MM/YYYY 并显示在单元格的 JTextField 中。

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?当我将它排序为字符串并且日期混淆时,如何更改该特定列的排序行为?

eg.例如。 after sorting in ASC order, I get this:按 ASC 顺序排序后,我得到:

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?我应该只为该特定列更改什么?

Because java.util.Date implements Comparable<Date> , it should be sufficient to let your TableModel return Date.class from getColumnClass() for column two.因为java.util.Date实现Comparable<Date> ,让您的TableModel从第二列的getColumnClass()返回Date.class应该就足够了。 Use a Custom Renderer to format the date as desired.使用自定义渲染器根据需要格式化日期。

Addendum: Here's an example using setDefaultRenderer() .附录:这是一个使用setDefaultRenderer()的示例。

import java.awt.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/** @see http://stackoverflow.com/questions/4553448 */
public class TableDate extends JPanel {

    private static final int INT_COL = 0;
    private static final int DATE_COL = 1;
    private final Calendar calendar = Calendar.getInstance();
    private final CustomModel model = new CustomModel();
    private final JTable table = new JTable(model);

    public TableDate() {
        super(new GridLayout(1, 0));
        table.setAutoCreateRowSorter(true);
        table.setDefaultRenderer(Date.class, new DateRenderer());
        table.setPreferredScrollableViewportSize(new Dimension(320, 240));
        JScrollPane sp = new JScrollPane(table);
        this.add(sp);
        for (int i = 1; i <= 20; i++) {
            model.addRow(newRow(i));
        }
    }

    private Object[] newRow(int i) {
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        return new Object[]{Integer.valueOf(i), calendar.getTime()};
    }

    private static class CustomModel extends DefaultTableModel {

        private final String[] columnNames = {"Index", "Date"};

        @Override
        public Class<?> getColumnClass(int col) {
            if (col == INT_COL) {
                return Integer.class;
            } else if (col == DATE_COL) {
                return Date.class;
            }
            return super.getColumnClass(col);
        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }
    }

    private static class DateRenderer extends DefaultTableCellRenderer {

        DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");

        public DateRenderer() {
            super();
        }

        @Override
        public void setValue(Object value) {
            setText((value == null) ? "" : formatter.format(value));
        }
    }

    private void display() {
        JFrame f = new JFrame("TableDate");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TableDate().display();
            }
        });
    }
}

您需要实现将日期字符串视为Date比较器,而不是简单的String请看这里

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

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