简体   繁体   English

Java JTable-首先单击列以降序排列

[英]Java JTable - First click on column to sort by descending

I have a Java JTable which stores about 8000 rows, with 10 columns, and all works well. 我有一个Java JTable,它存储大约8000行,10列,并且一切正常。

When I click on a column header, the whole JTable will be sorted by the data in that column in ascending order in the first instance, clicking again will then sort the data in descending order. 当我单击列标题时,整个JTable将在第一个实例中按该列中的数据按升序排序,再次单击则将按降序对数据排序。

I would like to change this behaviour on one specific column (double column - stores %'s) so that the first click will sort the data in descending order, and the second click will then be ascending - as I am most interested in the entries with the highest %s. 我想在一个特定的列(双列-存储%的列)上更改此行为,以便第一次单击将数据降序排列,然后第二次单击将升序-因为我对条目最感兴趣最高%s。

This is not about changing the original sort order when the table is first presented. 这与第一次显示表格时更改原始排序顺序无关。 The table is originally sorted by ID number (first column) ascending which is what I want. 该表最初是按ID编号(第一列)升序排序的,这正是我想要的。

This is about changing the behaviour when re-sorting based on other columns, so that the first click will be descending order. 这是关于更改基于其他列的重新排序时的行为,以便第一次单击将是降序排列。

Any one got any ideas? 任何人有任何想法吗?

    int noOfClicks = 1;
    public void arrange(){

TableRowSorter<TableModel> sorter = new TableRowSorter<>(table.getModel());
table.setRowSorter(sorter);
List<RowSorter.SortKey> sortKeys = new ArrayList<>();

int columnIndexToSort = 1;
    if (noOfClicks%2==0){ 
    //firstClick
    sortKeys.add(new RowSorter.SortKey(columnIndexToSort,   SortOrder.ASCENDING));

    }else{
   sortKeys.add(new RowSorter.SortKey(columnIndexToSort, SortOrder.DESCENDING));
    }
    ++noOfClicks;


sorter.setSortKeys(sortKeys);
sorter.sort();    

    }

you can do it with above approach.if you can store the noOfclicks then you can check whether you should arrange it by assending order or descending order. 您可以使用上述方法进行操作。如果可以存储noOfclicks,则可以检查是否应按升序或降序排列。 if the noOfclicks is a even number then you can arrange the table on assending order.no of clicks will be incremented by +1 on each click. 如果noOfclicks是偶数,则可以按升序排列表格。每次点击的点击次数都将增加+1。

I extended the TableRowSorter class to get the desired effect. 我扩展了TableRowSorter类以获得所需的效果。 In my case I wanted to sort all columns descending first, except for the first column. 在我的情况下,我想对所有列进行降序排序,第一列除外。 The following code allows to specify which columns should be sorted ascending first. 以下代码允许指定哪些列应首先升序排序。

/** Class sorting table descending on first click unless column is specified
 * in constructor to follow standard behaviour (ascending on first click).
 * @author Adam Jagosz */
class NormallyDescendingSorter extends TableRowSorter {

    /** columns to be sorted in a standard way, ascending on first click */
    ArrayList<Integer> ascendingColumns;

    /** Constructor
     * @param model table model to be sorted
     * @param ascendingColumns columns to follow ascending sort on first click */
    public NormallyDescendingSorter(TableModel model, int... ascendingColumns) {
        super(model);
        this.ascendingColumns = new ArrayList<>();
        for (int index = 0; index < ascendingColumns.length; index++)
        {
            this.ascendingColumns.add(ascendingColumns[index]);
        }
    }

    /**
     * Method sorting table rows upon clicking on column header
     * @param column column to sort by */
    @Override
    public void toggleSortOrder(int column) {
        if(ascendingColumns.contains(column)) {
            super.toggleSortOrder(column);
            return;
        }
        ArrayList<SortKey> sortKeys = new ArrayList<>(getSortKeys());
        if(sortKeys.isEmpty() || sortKeys.get(0).getColumn() != column) {
            sortKeys.add(0, new RowSorter.SortKey(column, SortOrder.DESCENDING));
        }
        else if (sortKeys.get(0).getSortOrder() == SortOrder.ASCENDING) {  
            sortKeys.removeIf(key -> key.getColumn() == column);
            sortKeys.add(0, new RowSorter.SortKey(column, SortOrder.DESCENDING));
        }
        else {
            sortKeys.removeIf(key -> key.getColumn() == column);
            sortKeys.add(0, new RowSorter.SortKey(column, SortOrder.ASCENDING));
        }
        setSortKeys(sortKeys);
    }
}

You can use this like that: 您可以这样使用:

DocumentTableModel model = new DocumentTableModel(document);
JTable table = new JTable(model);
NormallyDescendingSorter sorter = new NormallyDescendingSorter(model, 0);
table.setRowSorter(sorter);

(I specified 0 as column index to be sorted ascending). (我指定0作为要升序排序的列索引)。

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

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