I added the following code as a listener to JTableHeader:
header.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JTableHeader header = (JTableHeader) e.getSource();
JTable table = header.getTable();
TableColumnModel columnModel = table.getColumnModel();
int vci = columnModel.getColumnIndexAtX(e.getX());
int mci = table.convertColumnIndexToModel(vci);
if (mci == targetColumnIndex) {
TableColumn column = columnModel.getColumn(vci);
Object v = column.getHeaderValue();
boolean b = Status.DESELECTED.equals(v) ? true : false;
TableModel m = table.getModel();
for (int i = 0; i < m.getRowCount(); i++) m.setValueAt(b, i, mci);
column.setHeaderValue(b ? Status.SELECTED : Status.DESELECTED);
//header.repaint();
}
}
});
If I manually select one of more cells, then If I click the ckeckAll header, this code executes and then the visual order of rows is changing, thos selected jump to the bottom/top. ANy ideas?
The
TableHeaders in aJTablealready have a built in listener on them that sorts the values based on theComparatorfor the specificObjecttype stored in that column.You are adding a second listener to that table instead of overwriting the built-in one. So you are seeing the effects of both actions.
Update
If you don’t want any sorting capability, you can disable your row sorter, or just implement your own. You could also implement your own custom
TableColumns as well.