javaswingjpaneljradiobuttonbuttongroup

Enabling ButtonGroup


quick and easy question for those who know how to use Java, is there a way to enable/disable Button-group ? i know i can set it on Radio Button, but if i have a Button-group of selected Radio Buttons, is there a way to set Enabled to all or i have to do it one by one ? Thanks. What i did"i know that g1.setEnabled(true) is wrong but is there any way to make it work ?

r1 = new JRadioButton();
r2 = new JradioButton()
r3 = new JRadioButton();
g1 = new ButtonGroup();
g1.add(r1);
g1.add(r2);
g1.add(r3)
g1.setEnabled(true); 

Solution

  • There is no API in ButtonGroup class to enable/disable all the buttons. May be you can write a util method like this yourself:

    private static void enableButtonGroup(ButtonGroup buttonGroup, boolean enable)
    {
      Enumeration<AbstractButton> buttons = buttonGroup.getElements();
      while (buttons.hasMoreElements())
      {
        buttons.nextElement().setEnabled(enable);
      }
    }