Nimbus TableHeader was not highlighted as 'pressed'

前端 未结 1 1305
滥情空心
滥情空心 2021-01-06 01:20

The JTableHaeder has no \'pressed\' highlighting by default. (Nimbus)

NimbusDefaults says it has a default [Pressed] background painter.

What should I do, to

相关标签:
1条回答
  • 2021-01-06 01:43

    technically, you need that state on the rendering component, 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");
            }
        }
    

    Problem then is that the same instance (of rendering component) is used for all columns, so if you drag a column all appear pressed ...

    Edit: couldn't resist to dig a bit ... Nimbus is soooo ... lacking, to put it mildly ;-)

    Turns out that the defaults indeed have the styles for pressed, what's missing is the logic to set it. Probably not entirely trivial, because the logic (aka: MouseListener) resides in BasicTableHeaderUI which doesn't know about subclass' painter states. The only thingy the logic is supporting (hot needle fix) is rollover-awareness, but not pressed-ness.

    While we can't hook into the logic (well, we could ... but that's another trick :-) we can look for secondary state changes like draggingColumn/resizingColumn (not-bound) properties in JTableHeader and let a custom renderer update itself as appropriate. Here's a line-out of how-to:

    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);
            }
            // do similar for resizing column
            return comp;
        }
    
        public void setNimbusState(String state) {
            delegate.putClientProperty("Nimbus.State", state);
        }
    }
    
    0 讨论(0)
提交回复
热议问题