I have a simple JTable:
String[] columnNames = {"Freetext",
"Numbers only",
"Combobox"};
Object[][] data = {
{"Kathy", new Integer(21), "Female"},
{"John", new Integer(19), "Male"},
{"Sue", new Integer(20), "Female"},
{"Joe", new Integer(22), "Male"}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setAutoCreateRowSorter(true);
table.setFillsViewportHeight(true);
TableColumn comboboxCol = table.getColumnModel().getColumn(2);
JComboBox comboBox = new JComboBox();
comboBox.addItem("Male");
comboBox.addItem("Female");
comboboxCol.setCellEditor(new DefaultCellEditor(comboBox));
table.getColumnModel().getColumn(1).setCellEditor(new IntegerEditor(0, 100));
When I click on the colum header, it will alternate between ascending and descending sorting. I would like to add one more column header that will act differently on click, with other headers retaining their behaviour. How would you do that?
table.setAutoCreateRowSorter(true);
: This action defines a row sorter that is an instance ofTableRowSorter
. This provides a table that does a simple locale-specific sort when the user clicks on a column header. You can specify sort order and precedence of the column for sorting usingSortKeys
:Again, If you want to specify event on the specific column, for example column with id
bColumnID
:Edit:
however, I understood you wrong
(that upon one of the column header click you want the table unsorted and do other action) but as @camickr has made that clear
, use:sorter.setSortable(index, boolean)
.More formally, for turning off sorting for specific column with column identifier e.g.,
"bColumnName"
:to disable sorting for column with identifier
"bColumnName"
.