I have a JTable, that is sortable (made it sortable by calling setAutoCreateRowSorter(true) at initialization). I sort this table programmatically and I would like to disable the default event handling on table header, so that the table can be sorted programmatically only. How to achieve it?
The working piece of code would be:
public class SortTable extends JDialog {
private JTable table;
DefaultRowSorter<TableModel, String> sorter;
public SortTable() {
JScrollPane scrollPane = new JScrollPane();
setBounds(0, 0, 300, 200);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(scrollPane, BorderLayout.CENTER);
//-------most important stuff-------------------
table = new JTable();
table.setAutoCreateRowSorter(true); //enabling sorting
table.setModel(createModel());
sorter = (DefaultRowSorter<TableModel, String>)table.getRowSorter(); //store sorter to sort programatically later on
//-----------------------------------------------
scrollPane.setViewportView(table);
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
buttonPane.add(new JButton(getSortAction()));
}
private AbstractAction getSortAction() {
return new AbstractAction("Sort") {
@Override
public void actionPerformed(ActionEvent e) {
sorter.setSortKeys(Arrays.asList(new SortKey(0,SortOrder.ASCENDING)));
sorter.sort(); //sorting programatically
}
};
}
private DefaultTableModel createModel() {
return new DefaultTableModel(
new Object[][] {
{"1", "3"},
{"5", "2"},
{"4", null},
},
new String[] {
"A", "B"
}
);
}
}
This example is a JDialog containing a JTable with Sort button. Pressing that button will cause column A to be sorted ascending. However, the button is not the only way to sort the table - we can simply click the table header to change sorting. My question is how to make the button the only way to sort the table. It would be also nice to know how to get rid of the arrow that appears when sorting is changed.