开发者

Is there a way to customise Java's setEnabled(false)?

开发者 https://www.devze.com 2023-02-09 05:57 出处:网络
Let\'s say you have a JCheckBox you want to use as an on/off indicator. You use setEnabled(false) to disable mouse clicks on the JCheckBox. But setEnabled(false) also grays 开发者_如何学Goout the chec

Let's say you have a JCheckBox you want to use as an on/off indicator. You use setEnabled(false) to disable mouse clicks on the JCheckBox. But setEnabled(false) also grays 开发者_如何学Goout the checkBox and the checkBox's text. Is there a way to customise setEnabled(false) so that the graying out does not happen?

If that's not possible, is the only solution something along the lines of customising a ButtonModel?


You can subclass JCheckBox and override processMouseEvent/processKeyEvent to not do anything.

public class ReadOnlyCheckBox extends JCheckBox {
    public ReadOnlyCheckBox (String text, boolean selected) {
        super(text,selected);
    }

    protected void processKeyEvent(KeyEvent e) {
    }

    protected void processMouseEvent(MouseEvent e) {

    }
}


Extending the JCheckBox would let you override the setEnabled method, and pass your CustomJCheckBox to any code expecting a JCheckBox as a parameter. That should keep you from having to reimplement everything else from the check box, as you would for implementing a ButtonModel.

I am a little confused at what you're using the check box for -- it's not enabled, but you still want a check to appear or not, based on some other parameter? Why use an input element, in that case?


Changing the colour of text in a disabled JCheckbox has been requested and is still an open issue in the Sun Bug Database:

[6289684] Way to change the color of disabled JCheckbox text

Take a look at the bug details for a workaround.


You customize Button UI to do this.

0

精彩评论

暂无评论...
验证码 换一张
取 消