javaswingnetbeansjradiobutton

Generate actionPerformed event for a buttom group


Is it possible to Generate actionPerformed event for a ButtonGroup of radio buttons in Java 7?. I can not find this option in NetBeans. I'm working with GUI Builder. Or any other form to develop this task?

Right now I'm repeating code in all radio buttons, but I'm planning to add several of them and this approach would use to much code.


Solution

  • No, you can't add an ActionListener, or even a ChangeListener, to a ButtonGroup, but it's trivially easy to add ActionListeners to JRadioButtons, and if you create them in a for loop, as is often done, then there's no need for duplicate code.

    A general unrelated and unrequested suggestion: you may wish to avoid using code-generation software while you are still learning the GUI libray. The code generation library can shield you from having to understand what the library is doing under the covers, thus slowing the rate of your understanding of the library.