35

How can I detect when a JRadioButton is changed from 'unselected' to 'selected' when clicked with the mouse? I've tried using an ActionListener on the button, but that gets fired every time the radiobutton is clicked, not just when it's changing state to 'selected'.

I've thought of maintaining a boolean variable that remembers the state of the button, and test it inside the ActionListener to see whether to change its state but I'm wondering if there's a much better or cleaner solution.

cgull
  • 1,407
  • 1
  • 11
  • 18
  • Yes,an ActionListner is usually the way it is done. Everybody knows how to write an ActionListener because it is so common. The code simply would then simply use the isSelected() method of the button. The ItemListener fires multiple times as well so you have to check the ItemEvent to see whether this event was a select or deselect. I would stick with the more common approach. – camickr Sep 15 '09 at 03:35
  • Using an ActionListener like this still doesn't work as isSelected() returns true every time the radio button is clicked, which is what my initial problem was. – cgull Sep 15 '09 at 04:03
  • Works fine for me using JDK6 on XP. If the ChangeListener tells you the state has change, then the state better be changed or you have a big problem. Post your SSCCE showing the problem. – camickr Sep 15 '09 at 22:14
  • @camickr, you told him in your first comment to use an ActionListener and then when he explained there is a problem with that, you comment like you told him to use a ChangeListener. Both of those listeners have problems as pointed out below. – Nemi Aug 02 '12 at 16:15

2 Answers2

55

Look at JRadioButton.addItemListener()

EDIT: It is unlikely you want to use a changeListener as it fires multiple times per click. An itemListener fires only once per click. See here

EDIT2: Just to expand on this, an actionListener on a jradioButton will fire every time a user clicks on it, even if it is already selected. if that's what you want, fine, but I find it annoying. I only want to be notified it it is selected or deselected.

A ChangeListener will fire for all sorts of things, meaning your listener will receive 5 or more events per click. Not good.

An itemlistener will fire only if the selected or deselected state changes. This means that a user can click on it multiple times and it will not fire if it doesn't change. In your handler method you will have to have an if block checking for SELECTED or DESELECTED status and do whatever there:

@Override
public void itemStateChanged(ItemEvent e) {
    if (e.getStateChange() == ItemEvent.SELECTED) {
        // Your selected code here.
    }
    else if (e.getStateChange() == ItemEvent.DESELECTED) {
        // Your deselected code here.
    }
}

It just works better because you know that if you are in the method then the radio button has either just been selected or deselected, not that the user is just banging on the interface for some unknown reason.

Nemi
  • 3,011
  • 1
  • 26
  • 24
  • 3
    This works perfectly - despite the Sun documentation stating "Usually, you handle radio button clicks using an action listener". Thanks! – cgull Sep 15 '09 at 02:17
  • 1
    It would be really great if you could add a listener to a `ButtonGroup` that would only fire once upon a `JRadioButton` change within the group. – ryvantage Nov 28 '14 at 19:54
  • @Nemi Sorry but the edit link has dead. Can you provide how to use this approach? – Abraham Putra Prakasa Mar 20 '16 at 15:45
  • @AbrahamPutraPrakasa Sorry, after all this time I am not even sure what I linked to. Suffice to say, the EDIT2 section outlines everything I wanted to say about a ChangeListener firing multiple times per click. – Nemi Jul 25 '16 at 21:03
  • ---link is broken---- – Omore Dec 05 '17 at 21:56
  • 1
    Changed link to wayback machine – Nemi Apr 24 '18 at 16:32
0

I believe you want to add a ChangeListener implementation.

Rob Di Marco
  • 43,054
  • 9
  • 66
  • 56
  • 1
    As I mentioned in my post, adding a ChangeListener fires multiple times per click and is unlikely the best choice. – Nemi Sep 15 '09 at 02:21