我将以下代码添加为 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();
}
}
});
如果我手动选择多个单元格之一,然后如果我单击 ckeckAll 标题,则执行此代码,然后行的视觉顺序发生变化,选择跳到底部/顶部。有任何想法吗?