JCheckBox and RadioButton - potatoscript/JavaSwing GitHub Wiki
📝 JCheckBox and RadioButton in Java Swing 🎯
🎯 What is JCheckBox?
JCheckBox
is a component in Java Swing that allows users to select one or more options from a list. It provides a checkbox that can be checked or unchecked, commonly used in forms or settings to represent multiple options.
✅ Features of JCheckBox:
- Multiple selection: Users can select as many checkboxes as they want.
- State management: The checkbox can be in a checked or unchecked state.
📚 Step 1: Creating a Basic JCheckBox
📝 Example: Creating JCheckBox
import javax.swing.*;
public class JCheckBoxExample {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("JCheckBox Example");
// Create JCheckBoxes
JCheckBox option1 = new JCheckBox("Option 1");
JCheckBox option2 = new JCheckBox("Option 2");
// Set layout and add components
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(option1);
frame.add(option2);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
🎨 Explanation
✅ JCheckBox option1 = new JCheckBox("Option 1");
- Creates a checkbox labeled Option 1.
✅ frame.add(option1);
- Adds the checkbox to the frame.
👩🎨 Output:
+-------------------------------+
| [ ] Option 1 |
| [ ] Option 2 |
+-------------------------------+
🎯 Step 2: Handling JCheckBox State Change
To respond to checkbox state changes (checked or unchecked), you can add an ActionListener or ItemListener.
📝 Example: Handling JCheckBox Selection
import javax.swing.*;
import java.awt.event.*;
public class JCheckBoxSelection {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("JCheckBox Selection");
// Create JCheckBoxes
JCheckBox option1 = new JCheckBox("Option 1");
JCheckBox option2 = new JCheckBox("Option 2");
// Create JLabel to display selected options
JLabel label = new JLabel("Selected Options: None");
// Add ItemListener to update label when state changes
option1.addItemListener(e -> updateLabel(option1, option2, label));
option2.addItemListener(e -> updateLabel(option1, option2, label));
// Set layout and add components
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(option1);
frame.add(option2);
frame.add(label);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
// Method to update label based on selected options
private static void updateLabel(JCheckBox option1, JCheckBox option2, JLabel label) {
String selected = "Selected Options: ";
if (option1.isSelected()) {
selected += "Option 1 ";
}
if (option2.isSelected()) {
selected += "Option 2";
}
label.setText(selected.isEmpty() ? "Selected Options: None" : selected);
}
}
🎨 Explanation
✅ option1.addItemListener(e -> updateLabel(option1, option2, label));
- When the checkbox is checked or unchecked, the
updateLabel()
method is called to update the label based on the selected options.
👩🎨 Output:
+-------------------------------+
| [ ] Option 1 |
| [ ] Option 2 |
| Selected Options: None |
+-------------------------------+
(When Option 1 is checked):
+-------------------------------+
| [x] Option 1 |
| [ ] Option 2 |
| Selected Options: Option 1 |
+-------------------------------+
🎯 What is a RadioButton (JRadioButton)?
JRadioButton
is a component used for selecting one option from a group of choices. Unlike checkboxes, only one radio button in a group can be selected at a time. Radio buttons are often used in forms when you need to allow the user to select only one option from a set.
✅ Features of JRadioButton:
- Single selection: Only one radio button can be selected at a time in a group.
- Button grouping: Radio buttons must be grouped using a
ButtonGroup
.
📚 Step 3: Creating a Basic JRadioButton
📝 Example: Creating JRadioButton
import javax.swing.*;
import javax.swing.ButtonGroup;
public class JRadioButtonExample {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("JRadioButton Example");
// Create JRadioButtons
JRadioButton option1 = new JRadioButton("Option 1");
JRadioButton option2 = new JRadioButton("Option 2");
JRadioButton option3 = new JRadioButton("Option 3");
// Group radio buttons
ButtonGroup group = new ButtonGroup();
group.add(option1);
group.add(option2);
group.add(option3);
// Set layout and add components
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(option1);
frame.add(option2);
frame.add(option3);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
🎨 Explanation
✅ ButtonGroup group = new ButtonGroup();
- Groups the radio buttons together so only one can be selected.
✅ group.add(option1);
- Adds the radio button to the group.
👩🎨 Output:
+-------------------------------+
| ( ) Option 1 |
| ( ) Option 2 |
| ( ) Option 3 |
+-------------------------------+
🎯 Step 4: Handling JRadioButton Selection
To handle the selection of radio buttons, you can add an ActionListener.
📝 Example: Handling JRadioButton Selection
import javax.swing.*;
import java.awt.event.*;
public class JRadioButtonSelection {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("JRadioButton Selection");
// Create JRadioButtons
JRadioButton option1 = new JRadioButton("Option 1");
JRadioButton option2 = new JRadioButton("Option 2");
JRadioButton option3 = new JRadioButton("Option 3");
// Group radio buttons
ButtonGroup group = new ButtonGroup();
group.add(option1);
group.add(option2);
group.add(option3);
// JLabel to display selected option
JLabel label = new JLabel("Selected Option: None");
// Add ActionListener to radio buttons
option1.addActionListener(e -> label.setText("Selected Option: Option 1"));
option2.addActionListener(e -> label.setText("Selected Option: Option 2"));
option3.addActionListener(e -> label.setText("Selected Option: Option 3"));
// Set layout and add components
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(option1);
frame.add(option2);
frame.add(option3);
frame.add(label);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
🎨 Explanation
✅ option1.addActionListener(e -> label.setText("Selected Option: Option 1"));
- When Option 1 is selected, it updates the label with the selected option.
👩🎨 Output:
+-------------------------------+
| ( ) Option 1 |
| ( ) Option 2 |
| ( ) Option 3 |
| Selected Option: None |
+-------------------------------+
(When Option 1 is selected):
+-------------------------------+
| (x) Option 1 |
| ( ) Option 2 |
| ( ) Option 3 |
| Selected Option: Option 1 |
+-------------------------------+