0% found this document useful (0 votes)
2 views2 pages

Java Event Handling Basics Explained

The document explains event handling in Java, detailing how events are created in response to user actions within a graphical user interface. It describes the roles of event sources and event listeners, highlighting the ActionEvent as a common example. Additionally, it lists various types of events and their corresponding listeners, emphasizing the flexibility of event handling in Java applications.

Uploaded by

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

Java Event Handling Basics Explained

The document explains event handling in Java, detailing how events are created in response to user actions within a graphical user interface. It describes the roles of event sources and event listeners, highlighting the ActionEvent as a common example. Additionally, it lists various types of events and their corresponding listeners, emphasizing the flexibility of event handling in Java applications.

Uploaded by

Rupali Shinde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Event Handling in Java

-Rupali Shinde

What is an Event?
An event in Java is an object that is created when something changes
within a graphical user interface. If a user clicks on a button, clicks on a
combo box, or types characters into a text field, etc., then an event triggers,
creating the relevant event object. This behavior is part of Java's Event
Handling mechanism and is included in the Swing GUI library.

For example, let's say we have a JButton. If a user clicks on the JButton, a
button click event is triggered, the event will be created, and it will be sent
to the relevant event Listener (in this case, the ActionListener). The
relevant listener will have implemented code that determines the action to
take when the event occurs.

Note that an event source must be paired with an event listener, or its
triggering will result in no action.

How Events Work


Event handling in Java is comprised of two key elements:

● The event source, which is an object that is created when an event


occurs. Java provides several types of these event sources,
discussed in the section Types of Events below.
● The event listener, the object that "listens" for events and processes
them when they occur.

There are several types of events and listeners in Java: each type of event
is tied to a corresponding listener. For this discussion, let's consider a
common type of event, an action event represented by the Java class
ActionEvent, which is triggered when a user clicks a button or the item of a
list.

At the user's action, an ActionEvent object corresponding to the relevant


action is created. This object contains both the event source information
and the specific action taken by the user. This event object is then passed
to the corresponding ActionListener object's method:
void actionPerformed(ActionEvent e)

This method is executed and returns the appropriate GUI response, which
might be to open or close a dialog, download a file, provide a digital
signature, or any other of the myriad actions available to users in an
interface.

Types of Events
Here are some of the most common types of events in Java:

● ActionEvent: Represents a graphical element is clicked, such as a


button or item in a list. Related listener: ActionListener.
● ContainerEvent: Represents an event that occurs to the GUI's
container itself, for example, if a user adds or removes an object from
the interface. Related listener: ContainerListener.
● KeyEvent: Represents an event in which the user presses, types or
releases a key. Related listener: KeyListener.
● WindowEvent: Represents an event relating to a window, for
example, when a window is closed, activated or deactivated. Related
listener: WindowListener.
● MouseEvent: Represents any event related to a mouse, such as
when a mouse is clicked or pressed. Related listener: MouseListener.

Note that multiple listeners and event sources can interact with one
another. For example, multiple events can be registered by a single
listener, if they are of the same type. This means that, for a similar set of
components that perform the same type of action, one event listener can
handle all the events. Similarly, a single event can be bound to multiple
listeners, if that suits the program's design (although that is less common).

You might also like