views:

466

answers:

3

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.

+2  A: 

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

Nemi
This works perfectly - despite the Sun documentation stating "Usually, you handle radio button clicks using an action listener". Thanks!
cgull
A: 

I believe you want to add a ChangeListener implementation.

Rob Di Marco
As I mentioned in my post, adding a ChangeListener fires multiple times per click and is unlikely the best choice.
Nemi
A: 

Sun's tutorial on buttons, checkboxes, and radio buttons.

aberrant80
Going to that link was my first reaction also, but then I realized that the only example they give is using an ActionListener on a JRadioButton. :/
Nemi