0

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?

Mario B
  • 2,102
  • 2
  • 29
  • 41
  • 1
    unrelated: a) don't override paint, instead override paintComponent b) only override the paintComponent for custom painting c) **never-ever** change the component state in the paint methods. That said: Nimbus rarely respects the color properties as configured by its setters. Instead, provide a custom (per-component) skin property as explained f.i. in the [Nimbus related chapter](http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/custom.html) of the Swing bible and the articles referenced therein. – kleopatra Jan 22 '13 at 13:02

1 Answers1

0

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

Mario B
  • 2,102
  • 2
  • 29
  • 41