Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #4655

Re: Anybody know how to set the color of the text in a disabled JMenuItem?

From Knute Johnson <september@knutejohnson.com>
Newsgroups comp.lang.java.gui
Subject Re: Anybody know how to set the color of the text in a disabled JMenuItem?
Date 2011-08-20 14:16 -0700
Organization A noiseless patient Spider
Message-ID <j2p87c$khu$1@dont-email.me> (permalink)
References (2 earlier) <j2k5te$5lp$1@dont-email.me> <j2kfm1$tai$1@dont-email.me> <nospam-C33427.07014719082011@news.aioe.org> <j2m3kp$1c3$1@dont-email.me> <nospam-E55C9E.03452820082011@news.aioe.org>

Show all headers | View raw


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

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Anybody know how to set the color of the text in a disabled JMenuItem? Knute Johnson <september@knutejohnson.com> - 2011-08-18 11:39 -0700
  Re: Anybody know how to set the color of the text in a disabled JMenuItem? markspace <-@.> - 2011-08-18 14:38 -0700
    Re: Anybody know how to set the color of the text in a disabled JMenuItem? Knute Johnson <september@knutejohnson.com> - 2011-08-18 16:06 -0700
      Re: Anybody know how to set the color of the text in a disabled JMenuItem? markspace <-@.> - 2011-08-18 18:52 -0700
        Re: Anybody know how to set the color of the text in a disabled JMenuItem? "John B. Matthews" <nospam@nospam.invalid> - 2011-08-19 07:01 -0400
          Re: Anybody know how to set the color of the text in a disabled JMenuItem? Knute Johnson <september@knutejohnson.com> - 2011-08-19 09:39 -0700
            Re: Anybody know how to set the color of the text in a disabled JMenuItem? markspace <-@.> - 2011-08-19 10:49 -0700
            Re: Anybody know how to set the color of the text in a disabled JMenuItem? "John B. Matthews" <nospam@nospam.invalid> - 2011-08-20 03:45 -0400
              Re: Anybody know how to set the color of the text in a disabled JMenuItem? Knute Johnson <september@knutejohnson.com> - 2011-08-20 14:16 -0700
                Re: Anybody know how to set the color of the text in a disabled JMenuItem? "John B. Matthews" <nospam@nospam.invalid> - 2011-08-20 21:17 -0400
              Re: Anybody know how to set the color of the text in a disabled JMenuItem? Knute Johnson <september@knutejohnson.com> - 2011-08-20 14:28 -0700
        Re: Anybody know how to set the color of the text in a disabled JMenuItem? Tom <tom400f@gmail.com> - 2011-08-19 17:16 +0000
          Re: Anybody know how to set the color of the text in a disabled JMenuItem? Knute Johnson <september@knutejohnson.com> - 2011-08-19 11:03 -0700

csiph-web