Lecture 25 - Events and Listeners
Lecture 25 - Events and Listeners
1
● An event listener for a GUI event is an object belonging to a class that
implements one or more of the event-listener interfaces provided by the
`java.awt.event` and `javax.swing.event` packages.
● Both Swing and AWT components share many common event-listener types,
which are defined in the `java.awt.event` package.
● Additionally, Swing components have additional event-listener types specific
to them, which are defined in the `javax.swing.event` package.
Delegation
● An event listener object is responsible for "listening" to specific types of
events generated by event sources, usually GUI components, within a
program.
● An event handler, on the other hand, refers to a method that is invoked when
a particular type of event occurs.
● Each event-listener interface defines one or more event-handling methods
that must be implemented by the class that implements the event-listener
interface.
2
● It's important to note that interfaces define abstract methods, and any class
implementing an interface must provide definitions for all of its methods. If
not, the class itself becomes an abstract class and cannot be instantiated.
● The utilization of event listeners in event handling follows the delegation
event model, where the processing of an event is delegated to a specific
object, namely the listener, within the program.
● When an event takes place, the GUI component that the user interacted with
informs its registered listeners by invoking the corresponding event handling
method of each listener. This is called event multicasting
● For instance, if the user presses the Enter key in a JTextField, the
actionPerformed method of the registered listener will be called.
Example:
● Consider the following example of handling a button click event:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
3
// Register the event handler with the button
button.addActionListener(handler);
● In this example, we create a button using the `JButton` class from the Swing
library.
● We also create an event handler by implementing the `ActionListener`
interface, which has a single method called `actionPerformed(ActionEvent e)`.
● Inside the `actionPerformed` method, we define the actions we want to
perform when the button is clicked. In this case, we simply print a message to
the console saying "Button clicked!"
4
● Next, we register the event handler with the button using the
`addActionListener` method.
● This ensures that the `actionPerformed` method will be called when the
button is clicked.
● Finally, we create a `JFrame` to hold the button, set up the frame, and make
it visible so that we can interact with the button.
● When the button is clicked, the event source (button) will generate an event.
This event is then captured by the event handler (implemented as
`actionPerformed`) and the code inside the method is executed. In this case,
it prints "Button clicked!" to the console.