简体   繁体   English

Java-JTable-如何获取要排序的列和排序方向?

[英]Java - JTable - how to get which column is sorted and sort orientation?

is there any way how to get which column of JTable is sorted and what is sort orientation? 是否有任何方法可以获得JTable的哪一列排序以及排序方向是什么?

Thanks. 谢谢。

Use JTable.getRowSorter() to get the RowSorter. 使用JTable.getRowSorter()来获取RowSorter。

Then call getSortKey() . 然后调用getSortKey() The SortKey will tell you what you want to know. SortKey会告诉您您想知道的内容。

 /**
 * Returns an array with the indices of the sorted colummns. The array
 * returned is sorted from high priority to low priority. In case there 
 * are less than 3 sorted columns or there aren't RowSorter the values
 * in the returned array will be -1
 * @param table
 * @return array of length 3 with columns indices order by the highest priority descending. 
 */
public static int[] getTableSortedColumns(JTable table){
    int[] index = new int[]{-1,-1,-1};
    List<? extends SortKey> rowSorter = table.getRowSorter().getSortKeys();
    Iterator<? extends SortKey> it = rowSorter.iterator();
    int i = 0;
    while(it.hasNext()){
        SortKey sortKey = it.next();
        if(sortKey.getSortOrder().compareTo(SortOrder.UNSORTED)!=0 ){
            index[i] = sortKey.getColumn();
            i++;
        }
    }        
    return index;
}

/**
 * Return the sort orientation of a column.
 * @param table
 * @param columnIndex
 * @return int i == ascending, -1 == descending, 0 == unsorted 
 */
public static int getTableSortedOrientation(JTable table, int columnIndex){
    int[] indices = getTableSortedColumns(table);
    int orientation = 0;
    for(int i = 0;i<indices.length;i++){
        if(indices[i] == columnIndex){
            SortOrder so = table.getRowSorter().getSortKeys().get(i).getSortOrder();        
            if(so.compareTo(SortOrder.ASCENDING) == 0){
                orientation = 1;
            }else if(so.compareTo(SortOrder.DESCENDING) == 0){
                orientation = -1;
            }
        }
    }        
    return orientation;
}

Use Example : 使用示例:

int[] col = YourClass.getTableSortedColumns(jTable);
    for(int i = 0;i<col.length;i++){
        if(col[i]>=0){
            String orientation = "";
            switch(YourClass.getTableSortedOrientation(jTable, col[i])){
                case 1:
                    orientation = "Ascending";
                    break;
                case 0:
                    orientation = "Unsorted";
                    break;
                case -1:
                    orientation = "Descending";
                    break;
            }
            System.out.println("index "+col[i]+" "+orientation);
        }
    }
  • Go to the API doc for JTable 转到JTable的API文档
  • Use the browser text search features to look for "sort" 使用浏览器文本搜索功能查找“排序”
  • find JTable.getRowSorter() 找到JTable.getRowSorter()
  • read up the details there. 阅读那里的细节。

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

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