Event Handling: ItemListener, ActionListener, and Popup Events
Event Handling in Java: ItemListener, ActionListener और Popup Events
Event Handling in Java क्या है?
Java में Event Handling एक ऐसा mechanism है जो user के actions जैसे — button click, menu selection, key press या mouse movement को handle करने के लिए use होता है। जब भी user किसी GUI component (जैसे Button, Checkbox, JComboBox) पर कोई action perform करता है, तो उसे Event कहा जाता है, और जो code उस event पर react करता है, उसे Event Handler कहते हैं।
Java में events को handle करने के लिए java.awt.event package का use किया जाता है। इस package में कई interfaces और classes होती हैं जैसे ActionListener, ItemListener, MouseListener आदि।
Event Handling के प्रकार
Java में दो main types के event होते हैं:
- Foreground Events: जो user के direct action से trigger होते हैं (जैसे button click, menu select)।
- Background Events: जो system द्वारा generate किए जाते हैं (जैसे window closing, focus change)।
Event Delegation Model क्या होता है?
Java में Event Handling Event Delegation Model पर आधारित है। इसका मतलब है कि जब कोई event होता है, तो उसे उस object को भेजा जाता है जो उस event को handle करने के लिए register किया गया है।
इस model में तीन main components होते हैं:
- Event Source: वह component जो event generate करता है।
- Event Object: यह event की सारी जानकारी रखता है (जैसे कौन-सा component trigger हुआ, कौन-सा action हुआ आदि)।
- Event Listener: वह interface जो बताता है कि event होने पर क्या करना है।
ItemListener in Java
ItemListener interface का use तब किया जाता है जब हमें किसी item selection या deselection event को handle करना हो। यह interface java.awt.event package में मौजूद है।
Method of ItemListener
| Method | Description |
|---|---|
void itemStateChanged(ItemEvent e) |
जब भी कोई item select या deselect होता है, यह method call होता है। |
Example of ItemListener
import java.awt.*;
import java.awt.event.*;
class ItemExample extends Frame implements ItemListener {
Checkbox c1, c2;
Label l;
ItemExample() {
l = new Label("Select Language:");
l.setBounds(50, 50, 150, 30);
c1 = new Checkbox("Java");
c1.setBounds(50, 100, 80, 30);
c2 = new Checkbox("Python");
c2.setBounds(50, 140, 80, 30);
c1.addItemListener(this);
c2.addItemListener(this);
add(l); add(c1); add(c2);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
System.out.println("Selected: " + e.getItem());
}
public static void main(String args[]) {
new ItemExample();
}
}
ऊपर दिए गए example में जब भी user कोई checkbox select या deselect करता है, तो itemStateChanged() method automatically call होता है और selected item का नाम print होता है।
ActionListener in Java
ActionListener interface सबसे ज़्यादा use होने वाला listener है। इसका use तब किया जाता है जब कोई Action Event trigger हो जैसे कि button click या menu select।
Method of ActionListener
| Method | Description |
|---|---|
void actionPerformed(ActionEvent e) |
जब भी कोई action perform होता है, यह method call होता है। |
Example of ActionListener
import java.awt.*;
import java.awt.event.*;
public class ButtonExample extends Frame implements ActionListener {
TextField tf;
Button b;
ButtonExample() {
tf = new TextField();
tf.setBounds(60, 50, 170, 20);
b = new Button("Click Me");
b.setBounds(100, 120, 80, 30);
b.addActionListener(this);
add(b); add(tf);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
tf.setText("Button Clicked!");
}
public static void main(String[] args) {
new ButtonExample();
}
}
इस example में जब भी user “Click Me” button पर click करता है, तो actionPerformed() method run होता है और TextField में “Button Clicked!” show होता है।
ActionListener के Common Uses
- Button click event handle करने के लिए
- Menu item select करने पर
- Form submission event handle करने के लिए
Popup Events in Java
Popup Events वो events होते हैं जो किसी popup menu को show या hide करते समय trigger होते हैं। Java में इन्हें handle करने के लिए PopupMenuListener interface का use किया जाता है।
PopupMenuListener Methods
| Method | Description |
|---|---|
void popupMenuWillBecomeVisible(PopupMenuEvent e) |
जब popup menu visible होने वाला होता है, तब call होता है। |
void popupMenuWillBecomeInvisible(PopupMenuEvent e) |
जब popup menu hide होने वाला होता है, तब call होता है। |
void popupMenuCanceled(PopupMenuEvent e) |
जब popup menu cancel होता है, तब call होता है। |
Example of PopupMenuListener
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class PopupExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Popup Example");
JPopupMenu popup = new JPopupMenu("Edit");
JMenuItem cut = new JMenuItem("Cut");
JMenuItem copy = new JMenuItem("Copy");
JMenuItem paste = new JMenuItem("Paste");
popup.add(cut); popup.add(copy); popup.add(paste);
popup.addPopupMenuListener(new PopupMenuListener() {
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
System.out.println("Popup visible");
}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
System.out.println("Popup hidden");
}
public void popupMenuCanceled(PopupMenuEvent e) {
System.out.println("Popup canceled");
}
});
frame.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger())
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
frame.setSize(400, 400);
frame.setLayout(null);
frame.setVisible(true);
}
}
ऊपर दिए गए example में जब भी user right-click करता है, तो popup menu दिखाई देता है। जब popup show या hide होता है, तो corresponding method automatically call होता है।
ItemListener और ActionListener में अंतर
| Basis | ItemListener | ActionListener |
|---|---|---|
| Use | Selection और Deselection events handle करने के लिए | Action events (जैसे button click) handle करने के लिए |
| Method | itemStateChanged() |
actionPerformed() |
| Event Type | ItemEvent |
ActionEvent |
| Example Components | Checkbox, JComboBox | Button, MenuItem |
Event Handling के Practical Uses
- GUI applications में user actions को process करने के लिए।
- Form submission या data validation में।
- Interactive software और games में।
- Automation tools और admin panels में।
Important Points to Remember
- हर listener interface को implement करने पर उसका method override करना जरूरी होता है।
- Event handling Java Swing और AWT दोनों में similar तरीके से होती है।
- Anonymous classes या Lambda expressions से code को short बनाया जा सकता है।
- Multiple components के लिए same listener use किया जा सकता है।
Exam Notes
- ItemListener: Used for handling selection events.
- ActionListener: Used for handling action events like button clicks.
- PopupMenuListener: Used for handling popup show/hide events.
- Event Delegation Model: Divides event handling into source, listener, and event object.
- Core Methods:
itemStateChanged(),actionPerformed(),popupMenuWillBecomeVisible(). - Java Event Handling का base package:
java.awt.eventऔरjavax.swing.event.