1

I have two radio buttons, both added to a ButtonGroup. I have added an ActionListener for both. Suppose that at present the first radio button is selected, then again if I click on the same button then again actionPerformed() will be called. It doesn't look good. I want to prevent the call to actionPerformed() if that radio button is already selected.

One possible way could be to store current selected radio button state in a variable, but I want to know the java internal method for this.

Is there any method to do this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    You could try and have a look instead of adding event listener, using ItemStateChanged. One good example is here: http://stackoverflow.com/questions/1424738/detecting-a-jradiobutton-state-change – fasaas Nov 15 '16 at 11:04
  • 1
    BTW - the title is showing signs of an [XY problem](http://meta.stackexchange.com/q/66377). A better way to state the true purpose might be expressed in *"How to avoid performing action for already selected radio button?"* As to.. *"One possible way could be to store current selected radio button state in a variable"* I think that's the way to go here, None of `ActionListener`, `ItemListener` or `ChangeListener` seem to offer any hope of avoiding the firing of the event(s) or any method to check for actual change in the true/false state of the radio button. – Andrew Thompson Nov 15 '16 at 11:35

1 Answers1

2

if(radioButton.isSelected()) this will tell you if its selected. if it's selected you don't perform action. If it's not selected perform the action.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • Having already up-voted this, I just checked and it won't work as you expect. – Andrew Thompson Nov 15 '16 at 11:24
  • Simple, with that test being done, click the button twice to see it pass the test both times. .. Try it if you don't believe me. – Andrew Thompson Nov 15 '16 at 11:51
  • yes it passes the test both times cause it is always selected. that's what should happen and also what i expect – XtremeBaumer Nov 15 '16 at 12:04
  • But the OP needs to perform an action the *first* time it is selected. How does this approach distinguish between the 1st, 2nd, ..17th time the button is clicked? – Andrew Thompson Nov 15 '16 at 14:35
  • it doesn't need to. if he only wants it the first time. he says `if(!radioButton.isSelected())` then the following code will only be executed the first time the button gets clicked – XtremeBaumer Nov 15 '16 at 14:44
  • No, that's not what happened in my testing of it. Because it is in a **button group**, clicking the button the 2nd & subsequent times did not **deselect** it. – Andrew Thompson Nov 15 '16 at 15:25
  • that behavior is not requested as i understand the question. op needs to clarify the behavior he wants more – XtremeBaumer Nov 15 '16 at 15:27
  • Then you obviously fail to understand the question! The `ButtonGroup` is mentioned in the opening sentence. – Andrew Thompson Nov 15 '16 at 15:31
  • what i understand is: 2 radiobuttons, added to a ButtonGroup so only 1 can be selected. if now 1 of the buttons gets selected perform an action – XtremeBaumer Nov 15 '16 at 15:34