LECTURE 4 – EVENT AND LISTENER
(JAVA EVENT HANDLING)
Event in java AWT
An event is one of the most important concepts
in Java. It is change in the state of an object or behavior
by performing actions is referred to as an event. For
example, click on button, dragging mouse, etc. The
[Link].* package provides many event classes
and Listener interfaces for event handling.
CLASSIFICATION OF EVENTS
• Foreground Events
These are events that require user interaction to
generate. In order to generate these foreground
events, the user interacts with components in GUI.
When a user clicks on a button, moves the cursor,
and scrolls the scrollbar, an event be fired.
CLASSIFICATION OF EVENTS
• Background Events
These are events don’t require any user interaction.
These events automatically generate in the
background. OS failure, OS interrupts, operation
completion, etc., are examples of background
events.
DELEGATION EVENT MODEL
Java follows the Delegation Event Model for
handling the events. The Delegation Event Model
consist of Source and Listener. The Delegation Event
Model consist of Source and Listener.
DELEGATION EVENT MODEL
• Source
Buttons, checkboxes, list, menu-item, choice,
scrollbar, etc., are the sources from which events
are generated.
DELEGATION EVENT MODEL
• Listeners
The events which are generated from the source
are handles by the listeners. Each and every listener
represents interfaces that are responsible for
handling events.
We need to register the source with the
listener for handling events. Different types of
classes provide different registration methods.
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
• ActionEvent
The Java ActionListener is notified whenever you
click on the button or menu item. It is notified
against ActionEvent. The ActionListener interface is
found in the [Link] package. It has only one
method: actionPerformed().
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
• MouseEvent
The Java MouseListener is notified whenever you
change the state of mouse. It is notified against
MouseEvent. The MouseListener interface is found
in [Link] package. It has five
methods:mouseClicked(), mouseEntered(),
mouseExited(), mousePressed(), mouseReleased()
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
• MouseWheelEvent
MouseWheelEvent occurs when the mouse wheel
rotates in the component. mouseWheelMoved()
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
• KeyEvent
The Java KeyListener is notified whenever you
change the state of key. It has three methods:
keyPressed(), keyReleased(), keyTyped()
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
• ItemEvent
The Java ItemListener is notified whenever you click
on the checkbox. It is notified against ItemEvent. It
has only one method. itemStateChanged()
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
• TextEvent
The object of this class represents the text [Link]
TextEvent is generated when character is entered in the
text fields or text area. The TextEvent instance does not
include the characters currently in the text component
that generated the event rather we are provided with
other methods to retrieve that information.
textChanged().
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
• AdjustmentEvent
In Java AWT (Abstract Window Toolkit),
AdjustmentEvent is an event that indicates a change in a
scroll bar's value. This event is generated when the user
interacts with a scroll bar by adjusting its position,
either by clicking on the arrows, dragging the thumb, or
using the keyboard. Adjustment events are fundamental
for creating interactive GUI components like scroll bars
where user input dynamically changes the displayed
content.
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
• WindowEvent
In Java AWT (Abstract Window Toolkit),
WindowEvent represents an event that occurs
when a window-related action takes place, such as
opening, closing, activating, deactivating, or iconifying
a window.
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
• WindowEvent
In the windowOpened(WindowEvent e) method, “e”
(or any variable name you choose) is a local variable
representing the WindowEvent object that contains
information about the [Link] WindowEvent object a
allows you to retrieve details about the event that
occurred, although windowOpened typically doesn’t
require accessing these details directly. This is a standard
practice in Java event handling, where event listener
methods receive event objects providing context and
details about the event that triggered them.
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
• ComponentEvent
In Java AWT (Abstract Window Toolkit), ComponentEvent is
an event class that encapsulates events related to changes or
interactions with components in a graphical user interface.
ComponentEvent in Java AWT allows developers to respond
to changes in components within a graphical user interface.
By implementing ComponentListener or extending
ComponentAdapter, you can control how your application
responds to component events such as movement, resizing,
visibility changes, etc. This approach is fundamental for
creating interactive and responsive GUI applications in Java.
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
• ContainerEvent
In Java AWT (Abstract Window Toolkit), ContainerEvent
is an event class that encapsulates events related to
changes occurring within a container component.
Containers in Java AWT can include components like
Panel, Frame, Dialog, ScrollPane, etc., which can hold
other components (subcomponents).
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
• ContainerEvent
ContainerEvent in Java AWT allows developers to
respond to changes in the components within a
container (such as addition or removal of components).
By implementing ContainerListener or extending
ContainerAdapter, you can control how your
application responds to these container events. This
mechanism is essential for dynamic GUI applications
where components are added or removed based on
user interactions or application logic.
JAVA EVENT CLASSES,
LISTENER INTERFACES AND METHODS
• FocusEvent
In Java AWT (Abstract Window Toolkit), FocusEvent
is an event class that encapsulates events related to
keyboard focus changes in components within a
graphical user interface.
LET’S PRACTICE
(Happy Coding)