Better readability/contrast in a disabled JComboBox

fortran picture fortran · Jan 28, 2011 · Viewed 8k times · Source

I have a JComboBox that needs to be disabled at some point, but I am feeling that the disabled status makes it quite harder to read because the low contrast it has.

It would be nice if only the drop-down arrow button would be shown as disabled, while keeping the box renderer as if it were enabled.

Actual: actual combo Desired: desired result

Is there an easy way to achieve this or something similar?

Thanks!

Answer

fortran picture fortran · Jan 28, 2011

I've ended up peeking the BasicComboBoxUI, where I've found this:

        if ( comboBox.isEnabled() ) {
            c.setForeground(comboBox.getForeground());
            c.setBackground(comboBox.getBackground());
        }
        else {
            c.setForeground(DefaultLookup.getColor(
                     comboBox, this, "ComboBox.disabledForeground", null));
            c.setBackground(DefaultLookup.getColor(
                     comboBox, this, "ComboBox.disabledBackground", null));
        }

So I've used as renderer component a JLabel with the setForeground method overriden to do nothing. Thus, the colour is never changed and keeps the default black value.

The problem is that this trick is implementation specific. A given Look&Feel or UI Manager might do other things like overpainting with a semi-transparent layer to display disabled items instead of changing the component's colours :-(

Maybe a test could at least give a warning if the installed L&F or UI Manager does not call the setForeground method.