Search code examples
swinglook-and-feelnimbus

Swing: Nimbus L&F disabled combobox background-color


I have searched for an answer for days and read a lot about LookAndFeels and the opaque-issue, but couldn't find a solution. I am developing a Java Application with Swing, using NimbusLookAndFeel. I am satisfied with the look overall, but still want to modify a view things. Now i am stuck, because somehow i can't set the background-color of a disabled JCombobox (combobox.setEnabled(false);)

I already tried like a bazillion of different Properties with UIManager.put(..) + a lot of other things.

If i use another L&F something like this works:

combobox.setRenderer(new DefaultListCellRenderer() {
            @Override
            public void paint(Graphics g) {
                setBackground(Color.WHITE);
                setForeground(Color.BLACK);
                super.paint(g);
            }               
});

Any suggestions how to do this with Nimbus?


Solution

  • I played around with Nimbus defaults a lot. Modifying most components was no problem, but i couldn't change the background of any disabled component with it.

    I ended up writing a Custom ListCellRenderer like this

    public class DisabledListCellRenderer extends DefaultListCellRenderer {
        private static final long serialVersionUID = 1L;
        private JComponent component;
    
        public DisabledListCellRenderer(JComponent component) {
            this.component = component;
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            g.setColor(SwingHelper.disabledBackgroundColor);
            g.fillRect(0, 0, component.getSize().width, component.getSize().height);
            super.paintComponent(g);
        }
    }
    

    That finally worked, i am not sure if this a good solution though