I want to have a random radio button to be selected whenever this panel gets initialized, but I'm not sure how/if I can do that.
Is there a way to get a random button from the group and select it?
public class RandomPanel extends JPanel
private ButtonGroup buttonGroup;
private String buttonText =
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
buttonGroup = new ButtonGroup();
for (String text : buttonText)
JRadioButton option = new JRadioButton(text);
What you can do is keep a list/array of all the radio buttons you create, and then set the selected by using the button group's setSelected() method, something like this