开发者

JTable + Sorting specific field

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?

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?


Because java.util.Date implements Comparable<Date>, it should be sufficient to let your TableModel return Date.class from getColumnClass() for column two. Use a Custom Renderer to format the date as desired.

Addendum: Here's an example using 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();
            }
        });
    }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜