A small option: when I read the question, the main problem is the header, which does not update when the column selection changes. Having a custom header to listen to row selection changes doesn't help much in this scenario.
In fact, the JTableHeader is already listening on the ColumnModel, and notification of a model change includes selection changes. Only the columnSelectionChange method is intentionally implemented to do nothing:
// --Redrawing the header is slow in cell selection mode. // --Since header selection is ugly and it is always clear from the // --view which columns are selected, don't redraw the header.
A custom header can simply be implemented for redrawing (here, lazy, it does this in the factory table method, to save me the wiring to the table, you can easily make it a standalone class :-).
final JTable table = new JTable(new AncientSwingTeam()) { @Override protected JTableHeader createDefaultTableHeader() { // subclassing to take advantage of super auto-wiring // as ColumnModelListener JTableHeader header = new JTableHeader(getColumnModel()) { @Override public void columnSelectionChanged(ListSelectionEvent e) { repaint(); } }; return header; } }; table.setCellSelectionEnabled(true); table.getTableHeader().setDefaultRenderer(new ColumnHeaderRenderer());
Also slightly modified Mad renderer using the api table:
public static class ColumnHeaderRenderer extends DefaultTableCellHeaderRenderer { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean selected, boolean focused, int row, int column) { super.getTableCellRendererComponent(table, value, selected, focused, row, column); if (table.isColumnSelected(column)) { setBackground(table.getSelectionBackground()); } return this; } }
Regarding the observation:
always says isSelected is false
The reason is a small quirk in BasicTableHeaderUI:
ui.selected != columnModel.selected
uiSelected is the column that will be available for key binding - if laf supports it and the heading focusOwner. Actually this does not make sense, but by fully defining the semantics of ui and columnModel, you are thrilled about the new babe fx that is forgotten; -)