Event Handling for Radio Buttons: ChangeListener and Action Events
Event Handling for Radio Buttons: ChangeListener and Action Events
Java में जब हम Graphical User Interface (GUI) बनाते हैं, तो कई बार हमें user के choices capture करने होते हैं — जैसे “Male” या “Female” select करना, “Yes” या “No” choose करना आदि। ऐसे cases में Radio Buttons का use किया जाता है। लेकिन सिर्फ button दिखाना काफी नहीं है, हमें यह भी पता होना चाहिए कि user ने कौन सा option चुना। इसी को हम कहते हैं — Event Handling।
इस blog में हम step-by-step समझेंगे कि Radio Buttons के साथ ChangeListener और ActionListener का use करके events कैसे handle किए जाते हैं।
What is Radio Button in Java?
Radio Button एक GUI component होता है जो user को एक group में से सिर्फ एक option select करने की अनुमति देता है। Java में Radio Buttons को JRadioButton class से बनाया जाता है, जो javax.swing package का हिस्सा है।
JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
अगर हम चाहते हैं कि दोनों buttons एक group में रहें (यानि एक time पर सिर्फ एक ही select हो सके), तो हम ButtonGroup class का use करते हैं:
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(male);
genderGroup.add(female);
Event Handling Concept in Radio Buttons
जब भी user किसी Radio Button पर click करता है, तो एक event generate होता है। इस event को handle करने के लिए हम listener classes का use करते हैं। Java में दो major listeners Radio Buttons के लिए use किए जाते हैं:
- ActionListener — जब user किसी button पर click करता है।
- ChangeListener — जब button की state (selected/unselected) बदलती है।
Using ActionListener with Radio Buttons
ActionListener सबसे common method है Radio Buttons के events को handle करने के लिए। जब भी user किसी Radio Button पर click करता है, actionPerformed() method execute होता है।
Step-by-Step Example (ActionListener)
import javax.swing.*;
import java.awt.event.*;
public class RadioButtonExample extends JFrame implements ActionListener {
JRadioButton male, female;
ButtonGroup genderGroup;
JLabel result;
RadioButtonExample() {
setTitle("Radio Button Example");
setSize(300, 200);
setLayout(null);
male = new JRadioButton("Male");
female = new JRadioButton("Female");
male.setBounds(50, 30, 100, 30);
female.setBounds(50, 60, 100, 30);
genderGroup = new ButtonGroup();
genderGroup.add(male);
genderGroup.add(female);
result = new JLabel("Select Gender:");
result.setBounds(50, 100, 200, 30);
add(male);
add(female);
add(result);
male.addActionListener(this);
female.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == male)
result.setText("Selected: Male");
else if (e.getSource() == female)
result.setText("Selected: Female");
}
public static void main(String[] args) {
new RadioButtonExample();
}
}
ऊपर दिए गए example में, जब user किसी Radio Button पर click करता है, तो actionPerformed() method call होता है और label में selected gender दिखाया जाता है।
Important Points (ActionListener)
- हर Radio Button के साथ addActionListener() method लगाना जरूरी है।
- ActionEvent object बताता है कि कौन सा button click हुआ।
- ActionListener simple और beginners के लिए सबसे आसान तरीका है event handling का।
Using ChangeListener with Radio Buttons
ChangeListener तब use किया जाता है जब हमें Radio Button की state में change detect करना हो — यानी कब select हुआ या कब deselect। यह ItemListener की तरह काम करता है लेकिन थोड़ा और specific होता है।
ChangeListener का main method होता है stateChanged()। यह हर बार call होता है जब button की selection state बदलती है।
Example of ChangeListener
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class RadioButtonChangeExample extends JFrame {
JRadioButton yes, no;
JLabel msg;
RadioButtonChangeExample() {
setTitle("ChangeListener Example");
setSize(300, 200);
setLayout(new FlowLayout());
yes = new JRadioButton("Yes");
no = new JRadioButton("No");
ButtonGroup group = new ButtonGroup();
group.add(yes);
group.add(no);
msg = new JLabel("Select an option");
yes.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (yes.isSelected())
msg.setText("Selected: Yes");
}
});
no.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (no.isSelected())
msg.setText("Selected: No");
}
});
add(yes);
add(no);
add(msg);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new RadioButtonChangeExample();
}
}
Important Points (ChangeListener)
- हर बार जब button की state change होती है (select या deselect), stateChanged() method call होता है।
- यह ActionListener से थोड़ा low-level है क्योंकि यह हर change detect करता है।
- Useful है जब हमें real-time UI changes करने हों।
Difference Between ActionListener and ChangeListener
| Feature | ActionListener | ChangeListener |
|---|---|---|
| Event Trigger | जब user click करता है | जब button की selection state बदलती है |
| Use Case | Click actions के लिए | State change monitoring के लिए |
| Method | actionPerformed(ActionEvent e) | stateChanged(ChangeEvent e) |
| Complexity | Simple और direct | थोड़ा advanced |
| Performance | Lightweight | थोड़ा ज्यादा frequent calls |
Real-Life Example: Quiz Application
मान लीजिए हम एक Quiz Application बना रहे हैं जहाँ user को options चुनने होते हैं। यहाँ Radio Buttons perfect choice हैं। जब user किसी option को select करता है, तुरंत उसका answer validate हो सकता है।
if (optionA.isSelected()) {
score++;
}
ऐसे scenarios में हम ActionListener का use करते हैं क्योंकि हमें सिर्फ user के click की जरूरत होती है।
Best Practices for Radio Button Event Handling
- हर group के buttons को ButtonGroup में रखना ज़रूरी है ताकि केवल एक option select हो।
- Listeners को हमेशा अलग-अलग buttons पर लगाएं, ताकि event handling clear हो।
- UI update करने के लिए Swing thread-safe methods जैसे SwingUtilities.invokeLater() का use करें।
- Event logic को UI code से अलग रखें (MVC pattern follow करें)।
- अगर सिर्फ click detect करना है तो ActionListener use करें; state changes के लिए ChangeListener।
Advantages of Using Event Handling in Radio Buttons
- User interaction को track करना आसान होता है।
- Dynamic UI behavior implement किया जा सकता है।
- Program को responsive और interactive बनाया जा सकता है।
- Action और Change दोनों प्रकार के user inputs को handle किया जा सकता है।
Exam Tips and Notes
- JRadioButton — Radio Button बनाने के लिए use होता है।
- ButtonGroup — Buttons को group करने के लिए ताकि एक time पर सिर्फ एक select हो।
- ActionListener — Click event को handle करता है।
- ChangeListener — State change को detect करता है।
- Method Names याद रखें:
actionPerformed()औरstateChanged()। - Practical exam में ActionListener वाला example जरूर पूछते हैं।
- Use e.getSource() to identify which button was clicked।
- GUI बनाते समय setDefaultCloseOperation(EXIT_ON_CLOSE) लगाना न भूलें।
- Short programs याद कर लें क्योंकि Viva में अक्सर यही पूछते हैं।
Summary Table
| Listener | Purpose | Main Method | Triggered When |
|---|---|---|---|
| ActionListener | User clicks a button | actionPerformed(ActionEvent e) | On click |
| ChangeListener | Detects state change | stateChanged(ChangeEvent e) | On selection change |
अब आप पूरी तरह समझ चुके हैं कि Java में Radio Buttons के साथ Event Handling कैसे की जाती है। चाहे ActionListener हो या ChangeListener — दोनों का अपना specific use case है। Exam या practical में आप इन दोनों में से कोई भी approach confidently लिख सकते हैं।