I am having 2 JTables
, one for displaying the rows and one for displaying the data:
Problem: When the data table are sorted, also the rows are sorted:
Is it possible to make the "RowHeaderView" table to not get sorted like the data table?
public class SimpleJTableHeader extends JFrame {
public JTable table;
public SimpleJTableHeader() {
// CREATE A JTABLE
Object rowData[][] = { { "a" }, { "b" }, { "c" }, { "d" } };
Object columnNames[] = { "1" };
table = new JTable(rowData, columnNames);
table.setRowHeight(20);
table.setAutoCreateRowSorter(true);
JScrollPane scrollPane = new JScrollPane(table);
// ADD ROW NUMBERS TO THE TABLE
scrollPane.setRowHeaderView(getHeaderTable());
// SHOW TABLE
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
public JTable getHeaderTable() {
final AbstractTableModel model = new AbstractTableModel() {
private static final long serialVersionUID = 1L;
@Override
public int getColumnCount() {
return 1;
}
@Override
public Object getValueAt(int row, int column) {
return table.convertRowIndexToModel(row);
}
@Override
public int getRowCount() {
return table.getRowCount();
}
};
JTable headerTable = new JTable(model);
headerTable.setPreferredScrollableViewportSize(new Dimension(40, 0));
headerTable.setRowHeight(20);
return headerTable;
}
public static void main(String args[]) {
new SimpleJTableHeader();
}
}
Thanks to mKorbel the new correct getHeaderTable method: