This discussion is archived
7 Replies Latest reply: Jan 14, 2013 2:12 AM by TPD-Opitz-Consulting-com RSS

How to "listen" to a disabled JCheckBox?

984559 Newbie
Currently Being Moderated
Hi,

I have a checkbox that I'd like to behave as follows.

Initial state (enabled and de-selected)
1st click - selected (as normal)
2nd click - remain selected (but disabled)
3rd click - de-selected and re-enabled (back to initial state)


My action listener is able to perform these basic tasks.... but I can't figure out how to get the action event to fire while the JCheckBox is in the disabled state. That is to say, when the JCheckBox is disabled, clicking on it does not fire my ActionListener.

How do I override this and fire the ActionListener? Custom Action? If so, I don't know how to override the appropriate methods. Any advice is appreciated! Thanks in advance...
  • 1. Re: How to "listen" to a disabled JCheckBox?
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    981556 wrote:
    My action listener is able to perform these basic tasks.... but I can't figure out how to get the action event to fire while the JCheckBox is in the disabled state. That is to say, when the JCheckBox is disabled, clicking on it does not fire my ActionListener.
    Yes, that's the intended behavior...

    Maybe you should statr with an MVC-design now.
    http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

    Then you could manipulate the model rather than the (disabled) ChekckBox and you'd attach your Listener to the backing Model instead of the (disabled) CheckBox...

    bye
    TT
  • 2. Re: How to "listen" to a disabled JCheckBox?
    EJP Guru
    Currently Being Moderated
    I can't figure out how to get the action event to fire while the JCheckBox is in the disabled state. That is to say, when the JCheckBox is disabled, clicking on it does not fire my ActionListener.
    Of course not. There is no event. Nothing happened. Nothing to fire. It's disabled. You disabled it.
    >
    How do I override this and fire the ActionListener? Custom Action?
    You don't. Your question embodies a contradiction in terms. If you want the checkbox to be active and fire events, don't disable it. If you disable it, it won't fire events.

    If you want a click that can re-enable it, it will have to be on another component. It is not customary for users to be able to disable and re-enable UI elements. What are you doing exactly?
  • 3. Re: How to "listen" to a disabled JCheckBox?
    984559 Newbie
    Currently Being Moderated
    Thanks for the responses.

    I want to avoid having 21 sets of triple radio buttons (Currently have 21 checkboxes) as my UI doesn't have the real estate for this. And it would be overwhelming to look at.

    The checkboxes are used for inclusion criteria to narrow down data based on 21 boolean fields. I don't actually want to disable the checkboxes, I simply want to make them look different, grayed out being a very nice distinction. My intention is for checked (+enabled) to mean include logic, checked (+disabled) to mean exclude logic, and unchecked to be ignore logic (aka OR).

    At the moment, I recheck the checkbox upon the second click and change the label. I'd rather not change the label but instead change the component's appearance. I like the disabled look for this purpose.

    Can I gray it out without disabling it, then?

    Alternatively, can I add a separate MouseListener to "listen" to clicks on the checkbox, and programmatically only perform an action if it meets my criteria (disabled)?

    Surely there's a workaround...?

    Thanks again.
  • 4. Re: How to "listen" to a disabled JCheckBox?
    EJP Guru
    Currently Being Moderated
    The disabled look should be used when the component is disabled, and not otherwise. Think of something else.
  • 5. Re: How to "listen" to a disabled JCheckBox?
    984559 Newbie
    Currently Being Moderated
    I removed the ActionListener and used a MouseListener to successfully register a click on a disabled component. This works fine.

    That said, I do appreciate that the disabled look may be confusing, and that standards are standards for a reason... I ended up duplicating the checkbox's font and adding a strikethrough attribute:
    normalFont = jCheckBox.getFont();
    Map  attributes = normalFont.getAttributes();
    attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
    strikethroughFont = new Font(attributes);
    
    // and programattically choosing one of the following:
    jCheckBox.setFont(normalFont);
    jCheckBox.setFont(strikethroughFont);
    This has the visual distinction I was looking for, after all. Thanks for reminding me about GUI standards... :)
  • 6. Re: How to "listen" to a disabled JCheckBox?
    984559 Newbie
    Currently Being Moderated
    In summary:

    If trying to register a click on a disabled checkbox, the solution is a MouseListener, not an ActionEvent.

    The more proper solution in terms of GUI design is to strikethrough the label of the checkbox (via the font attribute).
  • 7. Re: How to "listen" to a disabled JCheckBox?
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    zredbaron wrote:
    The more proper solution in terms of GUI design is to strikethrough the label of the checkbox (via the font attribute).
    Or changing it's background color...

    bye
    TPD

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points