-
Notifications
You must be signed in to change notification settings - Fork 4
JRadioButton Java Swing Radio Button Example
In this post, I show you how to use JRadioButton class to create a Radio button in Swing-based application.
JRadioButton allows the user to select a single exclusive choice from a group of options. It is used with the ButtonGroup component.
The example has three radio buttons; the value of the selected radio button is shown in a status bar.
package net.sourcecodeexamples.swingexample.components2;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle;
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import static javax.swing.LayoutStyle.ComponentPlacement.RELATED;
public class RadioButtonExample extends JFrame
implements ItemListener {
private static final long serialVersionUID = 1 L;
private JLabel sbar;
private void initializeUI() {
JLabel lbl = new JLabel("Difficulty");
ButtonGroup group = new ButtonGroup();
JRadioButton rb1 = new JRadioButton("Easy", true);
JRadioButton rb2 = new JRadioButton("Medium");
JRadioButton rb3 = new JRadioButton("Hard");
group.add(rb1);
group.add(rb2);
group.add(rb3);
sbar = new JLabel("Selected: Easy");
rb1.addItemListener(this);
rb2.addItemListener(this);
rb3.addItemListener(this);
createLayout(lbl, rb1, rb2, rb3, sbar);
setSize(350, 250);
setTitle("Radio buttons");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void itemStateChanged(ItemEvent e) {
int sel = e.getStateChange();
if (sel == ItemEvent.SELECTED) {
JRadioButton button = (JRadioButton) e.getSource();
String text = button.getText();
StringBuilder sb = new StringBuilder("Selected: ");
sb.append(text);
sbar.setText(sb.toString());
}
}
private void createLayout(JComponent...arg) {
JPanel pane = (JPanel) getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(gl.createParallelGroup()
.addComponent(arg[0])
.addComponent(arg[1])
.addComponent(arg[2])
.addComponent(arg[3])
.addComponent(arg[4])
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(arg[1])
.addComponent(arg[2])
.addComponent(arg[3])
.addPreferredGap(RELATED,
GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(arg[4])
);
}
public static void main(String[] args) {
EventQueue.invokeLater(() - > {
RadioButtonExample radioButtonExample = new RadioButtonExample();
radioButtonExample.initializeUI();
radioButtonExample.setVisible(true);
});
}
}
Let's understand above program.
We created three JRadioButtons and placed into the ButtonGroup. The first radio button is preselected.
ButtonGroup group = new ButtonGroup();
JRadioButton rb1 = new JRadioButton("Easy", true);
JRadioButton rb2 = new JRadioButton("Medium");
JRadioButton rb3 = new JRadioButton("Hard");
group.add(rb1);
group.add(rb2);
group.add(rb3);
All three radio buttons share one ItemListener:
rb1.addItemListener(this);
rb2.addItemListener(this);
rb3.addItemListener(this);
When we select a radio button, two events are actually triggered: one for selection and one for deselection. We are interested in the selection.
if (sel == ItemEvent.SELECTED) {
}
We get the source of the event with the getSource() method and get the text label of the radio button:
JRadioButton button = (JRadioButton) e.getSource();
String text = button.getText();
We build the string and set it to the label:
StringBuilder sb = new StringBuilder("Selected: ");
sb.append(text);
sbar.setText(sb.toString());