Here you can find the source of setButtonSelected(ButtonGroup buttonGroup, int selected)
public static void setButtonSelected(ButtonGroup buttonGroup, int selected)
//package com.java2s; //License from project: Open Source License import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Collections; import javax.swing.AbstractButton; import javax.swing.ButtonGroup; public class Main { public static void setButtonSelected(ButtonGroup buttonGroup, int selected) { ArrayList<AbstractButton> buttons = Collections.list(buttonGroup.getElements()); if (buttons.isEmpty()) return; // Safety: The moron forgot to put radio buttons into this group! Don't select any! if (selected < 0 || selected >= buttons.size()) selected = 0; // Default is zero if you pass an out of range value. AbstractButton buttonSelected = buttons.get(selected); buttonSelected.setSelected(true); // Be consistent, when set, do this also: ActionEvent actionEvent = new ActionEvent(buttonSelected, ActionEvent.ACTION_PERFORMED, buttonSelected.getActionCommand()); for (ActionListener actionListener : buttonSelected.getActionListeners()) { actionListener.actionPerformed(actionEvent); }/*from w ww . j a va 2 s .c o m*/ } }