How to manage two JRadioButtons in java so that only one of them can be selected at a time

stillStudent picture stillStudent · Feb 12, 2010 · Viewed 47.7k times · Source

How to manage two JRadioButtons in java so that only one of them can be selected at a time? Is there any method in java to take care of this or you need to build your own logic?

Answer

ccheneson picture ccheneson · Feb 12, 2010

You have to add them in a ButtonGroup

ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);

Ensure you add this code after the buttons are created using the new JRadioButton constructors, as appropriate.