如何在JTable上设置自定义顺序

时间:2015-06-09 11:42:03

标签: java jtable jtableheader

我有一个带有自定义JTableModel的JTable,现在我想创建一个自定义比较器,因此用户可以使用我的自定义方法对所有列进行排序。

我有这段代码:

ColumnListener

public class ColumnListener extends MouseAdapter {
    protected JTable table;

    public ColumnListener(JTable t) {
        table = t;
    }

    public void mouseClicked(MouseEvent e) {
        TableColumnModel colModel = table.getColumnModel();
        int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
        int modelIndex = colModel.getColumn(columnModelIndex)
        .getModelIndex();

        if (modelIndex < 0)
            return;
        if (sortCol == modelIndex)
            isSortAsc = !isSortAsc;
        else
            sortCol = modelIndex;

        for (int i = 0; i < columnsCount; i++) { 
            TableColumn column = colModel.getColumn(i);
            column.setHeaderValue(getColumnName(column.getModelIndex()));
        }
        table.getTableHeader().repaint();

        Collections.sort(v,new MyComparator(isSortAsc,columnModelIndex));
        table.tableChanged(new TableModelEvent(MyTableModelSpese.this));
        table.repaint();
    }
}

MyComparator

class MyComparator implements Comparator {
    protected boolean isSortAsc;
    protected Integer numeroColonna;
    public MyComparator( boolean sortAsc,Integer numberColumn) {
        isSortAsc = sortAsc;
        numeroColonna = numberColumn;
    }

    public int compare(Object o1, Object o2) {
        if (!(o1 instanceof NotaSpese) || !(o2 instanceof NotaSpese))
            return 0;
        NotaSpese s1 = (NotaSpese) o1;
        NotaSpese s2 = (NotaSpese) o2;
        int result = 0;

        if(numeroColonna==1){
            //ordinamento per data
            result = s1.getDataOperazioneData().compareTo(s2.getDataOperazioneData());
        }

        if (!isSortAsc)
            result = -result;
        return result;
    }

    public boolean equals(Object obj) {
        if (obj instanceof MyComparator) {
            MyComparator compObj = (MyComparator) obj;
            return compObj.isSortAsc == isSortAsc;
        }
        return false;
    }
}

当我尝试订购一个列时,数据(列== 1),我看到订单后的集合

Collections.sort(v,new MyComparator(isSortAsc,columnModelIndex));

集合v中对象的顺序是可以的。 但是,如果我看到表中的元素没有按我的方法排序。 我看到了这个数据

2015-01-31
2015-12-30
2014-12-28

我该如何解决?

1 个答案:

答案 0 :(得分:2)

如果您使用MouseAdapter

,我认为您可以避免使用自定义TableRowSorter
TableRowSorter tableRowSorter = new TableRowSorter(tableModel.getModel());
//add your comparator to every column you want. in this case column with index 1
tableRowSorter.setComparator(1, new MyComparator(false));
table.setRowSorter(tableRowSorter);

您还可以对比较器进行参数化,因此您不必强制转换为NotaSpese

private class MyComparator implements Comparator<NotaSpese> {

    protected boolean isSortAsc;

    public MyComparator(boolean sortAsc) {
        isSortAsc = sortAsc;
    }

    @Override
    public int compare(NotaSpese o1, NotaSpese o2) {

        int result = o1.getDataOperazioneData().compareTo(o2.getDataOperazioneData());

        if (!isSortAsc) {
            result = -result;
        }
        return result;
    }
}