Re: Anybody know how to set the color of the text in a disabled JMenuItem?
In article <j2m3kp$1c3$1@dont-email.me>,
Knute Johnson <september@knutejohnson.com> wrote:
On 8/19/2011 4:01 AM, John B. Matthews wrote:
In article<j2kfm1$tai$1@dont-email.me>, markspace<-@.> wrote:
On 8/18/2011 4:06 PM, Knute Johnson wrote:
Thanks for that but apparently I asked the wrong question. Why
can't I change the foreground color on my JMenuItem like you can?
I'm running 1.7 on Windows XP. It may be something different with
the LookAndFeel.
I'm running Java 7 on Windows 7. My LNF is just the default one
(might be Synth). I was running within NetBeans 7.0.1 IDE. Can't
say why it doesn't work for you.
Have the related UI defaults for MenuItem.* changed?
I don't know.
<http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/>
I hadn't remembered that technique for changing the defaults but I only
want to change one JMenuItem's disabled foreground color. That defaults
do work just fine to change them all. I tried setting a new UI on the
JMenuItem but that doesn't seem to work either (see SSCCE below). When
I started this I didn't think it was going to take two days of messing
around with it :-).
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 B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>