import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;
class RadioButtonUtils {
public static Container createRadioButtonGrouping(String elements[], String title) {
JPanel panel = new JPanel(new GridLayout(0, 1));
if (title != null) {
Border border = BorderFactory.createTitledBorder(title);
panel.setBorder(border);
}
ButtonGroup group = new ButtonGroup();
JRadioButton aRadioButton;
for (int i = 0, n = elements.length; i < n; i++) {
aRadioButton = new JRadioButton(elements[i]);
panel.add(aRadioButton);
group.add(aRadioButton);
}
return panel;
}
}
public class GroupJRadioButton {
private static final String sliceOptions[] = { "4", "8", "12", "16" };
private static final String crustOptions[] = { "A", "B", "C", "D" };
public static void main(String args[]) {
JFrame frame = new JFrame("Grouping Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container sliceContainer = RadioButtonUtils.createRadioButtonGrouping(sliceOptions,
"Slice Count");
Container crustContainer = RadioButtonUtils.createRadioButtonGrouping(crustOptions,
"Crust Type");
frame.add(sliceContainer, BorderLayout.WEST);
frame.add(crustContainer, BorderLayout.EAST);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
|