📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Java Swing Radio Button Example
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 = 1L;
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);
});
}
}
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);
rb1.addItemListener(this);
rb2.addItemListener(this);
rb3.addItemListener(this);
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());
Output
Related Swing Examples
- Java Swing Exit Button - In this post, I show you how to exit a Swing application when clicking on the exit button.
- Swing ToolTip Tutorial with Example - In this tutorial, we will learn how to add tooltip text to a Swing component.
- Java Swing BorderLayout Example - In this example, we will learn how to use BorderLayout in GUI/swing based applications.
- Java Swing GridLayout Tutorial with Examples - In this tutorial, we will learn how to use GridLayout in GUI/swing based applications.
- Swing Mouse Move Events using MouseMotionAdapter - In this tutorial, we will learn how to receive mouse motion events using MouseMotionAdapter.
- Java Swing CheckBox Example - In this post, I show you how to use JCheckBox class to create a Radio button in a Swing-based application.
- Java Swing Radio Button Example - In this post, I show you how to use JRadioButton class to create a Radio button in a Swing-based application.
- Java Swing Progress Bar Example - In this post, I show you how to create a progress bar using the JProgressBar component in swing-based applications.
- Java Swing Combo Box Example - In this post, I show you how to create a combo box using a JComboBox component in swing-based applications.
- Java Swing Slider Example - In this post, I show you how to create a slider using the JSlider component in swing-based applications.
- Java Swing Toggle Button Example - In this post, we will learn how to create a toggle button using the JToggleButton component in swing-based applications.
Comments
Post a Comment
Leave Comment