1. What is Event-Driven Programming?
A programming paradigm where the program responds to user interactions (e.g., mouse
clicks, key presses).
Used extensively in GUI applications to make them interactive.
2. Key Components of Event Handling
Component Description Examples
Event Source The GUI component that generates an event. JButton, JTextField, JCheckBox
Event Object Encapsulates details about the event (e.g., which ActionEvent, MouseEvent
button was clicked).
Event Listener An interface that "listens" for events and defines ActionListener, ItemListener
methods to handle them.
3. Common Event Classes
ActionEvent: Button clicks, menu selections.
ItemEvent: Checkbox/radio button state changes.
MouseEvent: Mouse actions (clicks, movement).
KeyEvent: Keyboard interactions.
WindowEvent: Window operations (e.g., closing).
4. Steps to Handle Events
1. Define a Listener Class:
Implement a listener interface (e.g., ActionListener).
Override its methods (e.g., actionPerformed()).
2. Register the Listener:
Attach the listener to the event source.
button.addActionListener(this); // 'this' refers to the listener class
3. Handle the Event:
Write logic inside the listener method.
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
A programming paradigm where the program responds to user interactions (e.g., mouse
clicks, key presses).
Used extensively in GUI applications to make them interactive.
2. Key Components of Event Handling
Component Description Examples
Event Source The GUI component that generates an event. JButton, JTextField, JCheckBox
Event Object Encapsulates details about the event (e.g., which ActionEvent, MouseEvent
button was clicked).
Event Listener An interface that "listens" for events and defines ActionListener, ItemListener
methods to handle them.
3. Common Event Classes
ActionEvent: Button clicks, menu selections.
ItemEvent: Checkbox/radio button state changes.
MouseEvent: Mouse actions (clicks, movement).
KeyEvent: Keyboard interactions.
WindowEvent: Window operations (e.g., closing).
4. Steps to Handle Events
1. Define a Listener Class:
Implement a listener interface (e.g., ActionListener).
Override its methods (e.g., actionPerformed()).
2. Register the Listener:
Attach the listener to the event source.
button.addActionListener(this); // 'this' refers to the listener class
3. Handle the Event:
Write logic inside the listener method.
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}