Introduction to Radio Buttons: Single-Selection Control in Java GUI
Introduction to Radio Buttons: Single-Selection Control in Java GUI
जब हम Java GUI (Graphical User Interface) की बात करते हैं, तो एक बहुत important component होता है Radio Button। Radio Button एक ऐसा UI element है जो user को एक ही option select करने की अनुमति देता है, जब कई options दिए गए हों। Example के तौर पर, अगर आपको किसी form में Gender चुनना हो (Male/Female/Other), तो आप radio buttons का use करते हैं।
What is Radio Button in Java?
Java में Radio Button को implement करने के लिए JRadioButton class का use किया जाता है, जो javax.swing package के अंदर मौजूद है। Radio Button दिखने में छोटे circular button की तरह होता है। जब user किसी एक option को select करता है, तो बाकी options automatically deselect हो जाते हैं — इसे हम single-selection mechanism कहते हैं।
Key Points about Radio Button
- एक time पर केवल एक ही Radio Button select हो सकता है।
- Multiple Radio Buttons को group करने के लिए
ButtonGroupclass का use किया जाता है। - यह mostly forms, surveys या option-based questions में use होता है।
Syntax of JRadioButton in Java
नीचे JRadioButton बनाने का basic syntax दिया गया है:
JRadioButton rb = new JRadioButton("Option Name");
यह code एक नया radio button बनाता है जिसके label में “Option Name” लिखा होगा।
Creating and Displaying Radio Buttons in Java
आइए step-by-step समझते हैं कि Java GUI में Radio Buttons कैसे create और display किए जाते हैं:
Step 1: Import Required Packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Step 2: Create Frame and Components
Frame और components को declare करके उन्हें window में add किया जाता है।
class RadioExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Radio Button Example");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
rb1.setBounds(100, 50, 100, 30);
rb2.setBounds(100, 100, 100, 30);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
frame.add(rb1);
frame.add(rb2);
frame.setSize(300,200);
frame.setLayout(null);
frame.setVisible(true);
}
}
ऊपर दिए गए code में दो radio buttons बनाए गए हैं — “Male” और “Female”। ButtonGroup का use करके दोनों को group किया गया है ताकि एक time पर केवल एक option select हो सके।
ButtonGroup in Java
अगर आप चाहते हैं कि एक set के अंदर केवल एक ही option चुना जाए, तो आपको सभी radio buttons को ButtonGroup में जोड़ना होता है। यह ensure करता है कि multiple selection possible न हो।
Example:
ButtonGroup group = new ButtonGroup();
group.add(rb1);
group.add(rb2);
group.add(rb3);
अब rb1, rb2 और rb3 एक ही group के अंदर हैं — यानी एक select होगा तो बाकी automatically deselect हो जाएंगे।
Event Handling in Radio Button
जब user किसी radio button को select करता है, तो हमें उसके response को handle करना होता है। इसके लिए ActionListener interface का use किया जाता है।
Example of Event Handling:
import javax.swing.*;
import java.awt.event.*;
class RadioEventExample extends JFrame implements ActionListener {
JRadioButton rb1, rb2;
JButton btn;
RadioEventExample() {
rb1 = new JRadioButton("Java");
rb2 = new JRadioButton("Python");
btn = new JButton("Show");
rb1.setBounds(100, 50, 100, 30);
rb2.setBounds(100, 100, 100, 30);
btn.setBounds(100, 150, 80, 30);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
btn.addActionListener(this);
add(rb1); add(rb2); add(btn);
setLayout(null);
setSize(300,250);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(rb1.isSelected()) {
JOptionPane.showMessageDialog(this, "You selected Java");
} else if(rb2.isSelected()) {
JOptionPane.showMessageDialog(this, "You selected Python");
}
}
public static void main(String[] args) {
new RadioEventExample();
}
}
इस example में जब user “Show” button दबाता है, तो program check करता है कि कौन-सा radio button select हुआ है और उसके अनुसार message दिखाता है।
Common Properties and Methods of JRadioButton
| Method | Description |
|---|---|
setText(String t) | Button पर दिखने वाला text set करता है। |
isSelected() | Check करता है कि button selected है या नहीं। |
setEnabled(boolean b) | Button को enable या disable करता है। |
setActionCommand(String s) | Event पहचानने के लिए command assign करता है। |
addActionListener(ActionListener l) | Event listener जोड़ता है। |
Advantages of Using Radio Buttons
- User-friendly interface प्रदान करता है।
- Single choice selection को आसान बनाता है।
- Data input errors को reduce करता है।
- Simple validation logic के साथ easy to handle होता है।
Difference Between Radio Button and Checkbox
| Parameter | Radio Button | Checkbox |
|---|---|---|
| Selection Type | Single Selection | Multiple Selection |
| Grouping | ButtonGroup में रखा जाता है | Grouping optional है |
| Use Case | One choice out of many | Multiple independent choices |
| Example | Gender Selection | Hobbies Selection |
Use Cases of Radio Buttons in Java
- Registration Forms: Gender या Qualification चुनने के लिए।
- Quiz Applications: Single-answer multiple-choice questions के लिए।
- Settings Menus: Theme या Mode select करने के लिए।
- Feedback Forms: Rating या satisfaction level choose करने के लिए।
Best Practices for Using Radio Buttons
- Always group logically related radio buttons together।
- Default selection option provide करें ताकि user confusion न हो।
- Label text clear और short रखें।
- Event handling के लिए meaningful
actionCommandassign करें।
Summary of Radio Button in Java
Java में Radio Buttons एक powerful UI element हैं जो user को limited options में से एक select करने की सुविधा देते हैं। इन्हें JRadioButton और ButtonGroup के साथ use किया जाता है ताकि GUI applications में clean और user-friendly selection system बनाया जा सके। अगर आप Java GUI programming सीख रहे हैं, तो radio buttons का concept अच्छी तरह समझना बहुत जरूरी है, क्योंकि ये form-based applications में frequently use होते हैं।