有什么办法可以让JTable的哪一列被排序?排序方向是什么?
感谢。
答案 0 :(得分:4)
使用JTable.getRowSorter()
获取RowSorter。
然后拨打getSortKey()
。 SortKey
会告诉您您想知道的内容。
答案 1 :(得分:0)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK) {
return false;
}
return super.onKeyDown(keyCode, event);
}
使用示例:
/**
* 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;
}
答案 2 :(得分:-1)
JTable
JTable.getRowSorter()