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: Desired:
Is there an easy way to achieve this or something similar?
Thanks!
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.
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 .
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.
Try this **
UIManager.put( "ComboBox.disabledBackground", new Color(212,212,210) );
UIManager.put( "ComboBox.disabledForeground", Color.BLACK );
**
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With