Java JTable get column sort key
import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.RowSorter; import javax.swing.RowSorter.SortKey; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableColumn; import javax.swing.table.TableModel; public class Main extends JPanel { public static void main(String[] args) { JFrame f = new JFrame("JTable"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new Main()); f.pack();// w ww . j a v a 2 s. co m f.setVisible(true); } public Main() { TableModel dataModel = new MyTableModel(); JTable table = new JTable(dataModel); SortKey key = getSortKey(table,0); System.out.println(key); JScrollPane jsp = new JScrollPane(table); this.add(jsp); } public static SortKey getSortKey(JTable table, int column) { RowSorter rowSorter = table.getRowSorter(); if (rowSorter == null) { return null; } List sortedColumns = rowSorter.getSortKeys(); if (!sortedColumns.isEmpty()) { return (SortKey) sortedColumns.get(0); } return null; } } class MyTableModel extends AbstractTableModel { @Override public int getRowCount() { return 30; } @Override public String getColumnName(int i) { return "" + i; } @Override public int getColumnCount() { return 3; } @Override public Object getValueAt(int row, int col) { return Math.pow(row, col + 1); } @Override public Class<?> getColumnClass(int col) { return getValueAt(0, col).getClass(); } }