Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better readability/contrast in a disabled JComboBox

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!

like image 586
fortran Avatar asked Jan 28 '11 11:01

fortran


People also ask

How do I make my JComboBox unEditable?

u can make ur jcombobox uneditable by calling its setEnabled(false). A JComboBox is unEditable by default. You can make it editable with the setEditable(boolean) command. If you are talking about enabled or disabled you can set that by the setEnabled(boolean) method.

Which is the description of a JComboBox?

JComboBox is a part of Java Swing package. JComboBox inherits JComponent class . JComboBox shows a popup menu that shows a list and the user can select a option from that specified list . JComboBox can be editable or read- only depending on the choice of the programmer .

Which listener is implemented for JComboBox?

Adds a PopupMenu listener which will listen to notification messages from the popup portion of the combo box. Initializes the editor with the specified item. Sets the properties on this combobox to match those in the specified Action . This method is public as an implementation side effect.


4 Answers

Try this **

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

**

like image 91
thanhnbt Avatar answered Oct 20 '22 18:10

thanhnbt


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.

like image 25
fortran Avatar answered Oct 20 '22 17:10

fortran


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

like image 41
Costis Aivalis Avatar answered Oct 20 '22 17:10

Costis Aivalis


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.

like image 29
Antal Spector-Zabusky Avatar answered Oct 20 '22 18:10

Antal Spector-Zabusky