JTable列排序代码如下:
/**
* 填充数据并排序后显示
* @param table
* @param tableHead
* @param data
*/
public static void changeAndSortTable(JTable table, Object[] tableHead, Object[][] data){
@SuppressWarnings("serial")
DefaultTableModel model = new DefaultTableModel(data, tableHead){
@SuppressWarnings({ "unchecked", "rawtypes" })
public Class getColumnClass(int column){
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
};
RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
}