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!

有帮助吗?

解决方案

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.

其他提示

Here is another option you have:

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

You will just need to add this code after the instantiation. The letters will always stay black. The combo box frame will turn to be either gray or black if you disable or enable.

They look like this:

enter image description here

Here's another hack, due to Michael Grimes, which shouldn't be affected by the particular look and feel. The trick is to make the combo box editable; the JTextField which is exposed as the editor supports the setDisabledTextColor method. And since you're disabling the combo box, it doesn't matter that it's editable! The code that I'm using to do this (translated from Scala) is the following:

JComboBox cb = ...;
...
cb.setEditable(true);
ComboBoxEditor editor = cb.getEditor()
JTextField     etf    = (JTextField)etf.getEditorComponent()
etf.setDisabledTextColor(UIManager.getColor("ComboBox.foreground"));
etf.setBackground(UIManager.getColor("ComboBox.background"));
// editor.setItem(format(obj));
cb.setEnabled(false);

The cast is guaranteed to succeed here because we're using a BasicComboBoxEditor, whose docs say "The editor is implemented as a JTextField." The commented-out line occurs because I'm using a custom renderer which prints integers with extra text surrounding them; calling setItem allows me to specify a similar string, and is necessary because the editor ignores the custom renderer. If you're using the default renderer, then you don't need to worry about that line; on the other hand, if you're using a more complicated renderer, then you might need to do something else entirely.

Despite the fact that this is a horrific kludge, it works, and it doesn't seem to rely on any implementation-defined features. The two places I could imagine this breaking are (a), if an editable combo box looks very different from an uneditable one (for instance, my first attempt didn't change the background color of the text field, which made it look wrong), or (b) if BasicComboBoxEditor stopped returning a JTextField (which seems less likely). But so far, it's serving my purposes.

Try this **

UIManager.put( "ComboBox.disabledBackground", new Color(212,212,210) );
UIManager.put( "ComboBox.disabledForeground", Color.BLACK );

**

The result can be achieved with the following code:

    Component editorComponent = comboBox.getEditor().getEditorComponent();
    if(editorComponent instanceof JTextComponent){
        ((JTextComponent)editorComponent).setDisabledTextColor(Color.black);
    }

I did not test it with several L&F, but it might make a difference, as this fires a PropertyChange event ("disabledTextColor"). Please see the docs.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top