8 Replies Latest reply on Sep 5, 2008 7:14 PM by darrylburke

    How to disable a JCheckBox and leave the text the original color?

    843806
      When a JCheckBox is disabled, its text gets dim (gray). I need to just dim the checkbox but leave the text the original color.

      I can't find the code where the text color changes when the box is disabled...

      (I have found the code that draws the square and the box at: com.sun.java.swing.plaf.windows.WindowsIconFactory .)

      Any ideas?
        • 1. Re: How to disable a JCheckBox and leave the text the original color?
          darrylburke
          To do this for all check boxes in your application, you can try this code, which should be placed to execute before any JCheckBox is constructed.
          UIManager.put("CheckBox.disabledText", UIManager.get("CheckBox.foreground"));
          db
          • 2. Re: How to disable a JCheckBox and leave the text the original color?
            843806
            Thanks Darryl, that works!

            BTW, how did you find out the answer? is is documented anywhere?
            • 3. Re: How to disable a JCheckBox and leave the text the original color?
              843806
              Correction: this property works for the Metal LAF UI, but not for the Windows (XP) LAF...too bad, since we use only the later...
              • 4. Re: How to disable a JCheckBox and leave the text the original color?
                843806
                Try this checkbox, i think you could customize to achieve exact behaviour:
                import java.awt.Graphics;
                
                import javax.swing.JCheckBox;
                import javax.swing.JToggleButton;
                
                public class KCheckBox extends JCheckBox
                {
                
                    private boolean clickable;
                    private boolean enabled2;
                
                
                    /**
                     *
                     */
                    public KCheckBox()
                    {
                        super();
                        init();
                    }
                    /**
                     * @param text
                     */
                    public KCheckBox( String text )
                    {
                        super( text );
                        init();
                    }
                
                
                    private void init()
                    {
                        setModel( new MyButtonModel() );
                        this.clickable = true;
                        this.enabled2 = true;
                    }
                
                    public void paint( Graphics g )
                    {
                        // store existing state
                        boolean e = ((MyButtonModel)getModel()).isEnabled();
                
                        // if !enabled - set not enabled
                        // otherwise - set enabled
                        ((MyButtonModel)getModel()).setEnabledNoFire( this.enabled2 );
                
                        super.paint( g );
                
                        // restore previous state
                        ((MyButtonModel)getModel()).setEnabledNoFire(e);
                    }
                
                    public void setClickable(boolean clickable)
                    {
                        this.clickable = clickable;
                        super.setEnabled(clickable && this.enabled2);
                    }
                
                    public boolean isClickable()
                    {
                        return this.clickable;
                    }
                
                    public void setEnabled(boolean enabled)
                    {
                        this.enabled2 = enabled;
                        super.setEnabled(enabled && this.clickable);
                    }
                
                    public boolean isEnabled()
                    {
                        return this.enabled2;
                    }
                
                    private class MyButtonModel extends JToggleButton.ToggleButtonModel
                    {
                
                        public void setEnabledNoFire( boolean b )
                        {
                            if (b) {
                                stateMask |= ENABLED;
                            } else {
                                stateMask &= ~ENABLED;
                            }
                        }
                    }
                }
                Edited by: unvadim on Sep 5, 2008 4:41 PM
                • 5. Re: How to disable a JCheckBox and leave the text the original color?
                  darrylburke
                  cescolar wrote:
                  Thanks Darryl, that works!
                  Tested before posting.
                  BTW, how did you find out the answer? is is documented anywhere?
                  See the API for UIManager and UIDefaults and you can write a small routine that sysouts (or dumps to a file) all the UI defaults. Apart from that you can read the sources to find what keys are used.

                  db
                  • 6. Re: How to disable a JCheckBox and leave the text the original color?
                    darrylburke
                    Correction: this property works for the Metal LAF UI, but not for the Windows (XP) LAF...too bad, since we use only the later...
                    Well, it might have helped if you'd said that upfront.

                    I don't know whether there's an easy fix for the Windows LAF, but it appears you have a different approach suggested by unvadim.

                    db

                    edit But unvadim's code should probably override paintComponent and not paint.

                    edit2: In the OP, you said
                    When a JCheckBox is disabled, its text gets dim (gray).
                    With Windows LAF, disabled text has an etched effect. It is neither dim nor gray.

                    Edited by: Darryl.Burke
                    • 7. Re: How to disable a JCheckBox and leave the text the original color?
                      794342
                      but not for the Windows (XP) LAF..
                      wrap the text in html tags
                      JCheckBox cbx = new JCheckBox("<html>Disabled</html>",true);
                      • 8. Re: How to disable a JCheckBox and leave the text the original color?
                        darrylburke
                        Michael_Dunn wrote:
                        wrap the text in html tags
                        Thanks Michael. I would never have thought it could be as simple as that.

                        db