technically, you need this state on the rendering component, and not on the JTableHeader itself:
@Override public void mousePressed( MouseEvent e ) { JComponent source = (JComponent)e.getComponent(); source.putClientProperty( "Nimbus.State", "Pressed" ); if (source instanceof JTableHeader) { ((JComponent) ((JTableHeader) source).getDefaultRenderer()) .putClientProperty("Nimbus.State", "Pressed"); } }
The problem is that the same instance (rendering component) is used for all columns, so if you drag a column, everyone appears clicked ...
Edit: couldn't resist to dig a little ... Nimbus is soooo ... not enough, to say the least ,-)
It turns out that by default there really are styles for clicking, that there is no logic for setting it. Probably not entirely trivial, because the logic (aka: MouseListener) is in BasicTableHeaderUI, which does not know about the states of the subclass. The only thing that logic supports (hot-needle correction) is rollover awareness, but not tapping.
While we canβt connect to the logic (well, we could ... but this is another trick :-), we can look for secondary state changes, such as draggingColumn / resizingColumn (unrelated) properties in the JTableHeader, and allow user rendering to update itself by oneself. Here is a usage example:
public static class WrappingRenderer implements TableCellRenderer { private DefaultTableCellHeaderRenderer delegate; private JTableHeader header; public WrappingRenderer(JTableHeader header) { this.header = header; this.delegate = (DefaultTableCellHeaderRenderer) header.getDefaultRenderer(); header.setDefaultRenderer(this); } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component comp = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); TableColumn draggedColumn = table.getTableHeader().getDraggedColumn(); if (draggedColumn != null) { if (table.convertColumnIndexToModel(column) == draggedColumn.getModelIndex()) { setNimbusState("Pressed"); } else { setNimbusState(null); } } else { setNimbusState(null); }