3

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 out 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?

Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
Arvanem
  • 1,043
  • 12
  • 22
  • 2
    if you want that for all your checkboxes, change the Look and Feel. TO disable mouse clicks, you can also add a transparent component above it (you should also disable receiving focus or the user could use the keyboard for checking it). – mihi Feb 07 '11 at 17:26
  • This is a common issue with UI frameworks. The disabled (read-only) state is not very readable. I'm interested in what others do to address this. – Steve Kuo Feb 07 '11 at 17:28
  • 2
    Use a different UI element, possibly a JLabel with changing text, or something else. Check boxes are "reserved elements" in usability terms. Using a checkbox as an indicator that can't be used to change the setting is analogous to using #define to make C look like a different language. Sure, it's legal, but why would you ever cause users to go through that pain? – jprete Feb 07 '11 at 17:43

4 Answers4

5

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) {

    }
}
nos
  • 223,662
  • 58
  • 417
  • 506
2

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?

Thorn G
  • 12,620
  • 2
  • 44
  • 56
  • To clear up the confusion - JCheckBox's appearance is useful and clear for indicating whether a setting has been set or not. Yes it is an input element but the idea is to disable input. – Arvanem Feb 07 '11 at 17:33
  • I feel as though you might run into issues with user confusion by not clearly indicating this checkbox won't respond to clicks. Any reason you can't use some sort of enabled/disabled icon, instead? – Thorn G Feb 07 '11 at 19:21
2

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.

dogbane
  • 266,786
  • 75
  • 396
  • 414
0

You customize Button UI to do this.

rancidfishbreath
  • 3,944
  • 2
  • 30
  • 44