Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

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

843806Sep 4 2008 — edited Sep 5 2008
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?

Comments

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
843806
Thanks Darryl, that works!

BTW, how did you find out the answer? is is documented anywhere?
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...
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
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
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
794342
but not for the Windows (XP) LAF..
wrap the text in html tags
JCheckBox cbx = new JCheckBox("<html>Disabled</html>",true);
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
1 - 8
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Oct 3 2008
Added on Sep 4 2008
8 comments
6,487 views