0% found this document useful (0 votes)
30 views5 pages

Lecture 25 - Events and Listeners

The event-handling model in Java consists of three main components: the event source, the event object, and the event listener, which work together to manage user interactions with GUI components. Programmers must register event listeners and implement event-handling methods to respond to events, following the delegation event model where the event processing is delegated to specific listener objects. An example illustrates handling a button click event using the ActionListener interface to define actions performed when the button is clicked.

Uploaded by

alfredjoso847
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

Lecture 25 - Events and Listeners

The event-handling model in Java consists of three main components: the event source, the event object, and the event listener, which work together to manage user interactions with GUI components. Programmers must register event listeners and implement event-handling methods to respond to events, following the delegation event model where the event processing is delegated to specific listener objects. An example illustrates handling a button click event using the ActionListener interface to define actions performed when the button is clicked.

Uploaded by

alfredjoso847
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Event handling model

● The event-handling mechanism in Java consists of three main components:


the event source, the event object, and the event listener.
● The event source refers to the GUI component that the user interacts with.
● The event object encapsulates information about the occurred event,
including a reference to the event source and any specific details required by
the event listener to handle the event.
● The event listener is an object that gets notified by the event source when an
event happens.
● It receives an event object and uses it to respond to the event.
● To process a graphical user interface event in a Java program, the
programmer needs to accomplish two tasks.
a. First, they need to register an event listener for the GUI component
that is expected to generate the event.
b. Second, they must implement an event handling method or a set of
event-handling methods, commonly referred to as event handlers.

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;

public class ButtonClickExample {


public static void main(String[] args) {
// Create a button
JButton button = new JButton("Click me");

// Create an event handler


ActionListener handler = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Perform actions when the button is clicked
System.out.println("Button clicked!");
}
};

3
// Register the event handler with the button
button.addActionListener(handler);

// Create a JFrame to hold the button


JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(button);
frame.pack();
frame.setVisible(true);
}
}

● 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.

You might also like