0% found this document useful (0 votes)
16 views

Unit2 Event Handling 1

Event handling in Java allows programs to respond to user and system events. There are several types of events including those generated by mouse, keyboard, and GUI controls. The delegation event model defines how events are generated and processed - the event source generates an event which is sent to registered listener objects. Listeners must implement specific interfaces that define methods to handle different event types.

Uploaded by

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

Unit2 Event Handling 1

Event handling in Java allows programs to respond to user and system events. There are several types of events including those generated by mouse, keyboard, and GUI controls. The delegation event model defines how events are generated and processed - the event source generates an event which is sent to registered listener objects. Listeners must implement specific interfaces that define methods to handle different event types.

Uploaded by

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

Event Handling in Java

• Event handling is fundamental to Java programming because it is


integral to create applets and other types of GUI-based programs.

•Many program that uses a graphical user interface, such as a Java


application written for Windows, is event driven.

•Event handling in Java is the procedure that controls an event and


performs appropriate action if it occurs. The code or set of
instructions used to implement it is known as the Event handler.

It consists of two major components:


•The event source
•The event listener
The source is the object where the event occurs, and the event
listener is responsible for taking appropriate actions when an event
occurs.
•There are several types of events.
• The most commonly handled events are those generated by
the mouse, the keyboard, and various controls, such as a
push button.
• Events are supported by the java.awt.event package.

Events in Java
Events in Java represent the change in the state of any object. Events
occur when the user interacts with the interface. Clicking a button,
moving the mouse, typing a character, selecting an item from a list,
and scrolling the page are all examples of behaviors that cause an
event to occur.
Event handling in Java is the process of controlling an event and
taking appropriate action if one occurs.
Types of Events in Java:
•Foreground Events: These events necessitate the user's
direct participation. They are produced as a result of a user
interacting with graphical components in a Graphical User
Interface.

•Background Events: These events don’t require interactions


of users. Operating system interrupts and hardware or
software failures are examples of background events.
Event Handling Mechanism
The Delegation Event Model
• The modern approach to handle events is based on the
delegation event model.
• The delegation event model defines standard and consistent
mechanisms to generate and process events.
• Its concept is quite simple: a source generates an event and
sends it to one or more listeners. In this scheme, the listener
simply waits until it receives an event.
• Once received, the listener processes the event and then
returns.
• A user interface element is able to “delegate” the processing
of an event to a separate piece of code.
• In the delegation event model, listeners must register with a
source in order to receive an event notification because,
notifications are sent only to listeners that want to receive
them.
• Earlier, an event was propagated up the containment
hierarchy until it was handled by a component.
• This required components to receive events that they did not
process, and it wasted valuable time.
Fig. Delegation Event Model
Events
• In the delegation model, an event is an object that describes a state
change in a source.
• It can be generated as a consequence of a person
interacting with the elements in a graphical user interface,
such as pressing a button, entering a character via the
keyboard, selecting an item in a list, and clicking the mouse.
• Events may also occur that are not directly caused by
interactions with a user interface such as when a timer
expires, a counter exceeds a value, a software or a hardware
failure occurs.
Event Sources
• An event source is an object that generates an event.
Sources may generate more than one type of event.
• A source must register listeners in order for the listener to
receive notifications about a specific type of event. Each type
of event has its own registration method. Here is the general
form:
public void addTypeListener(TypeListener el)
• Here, Type is the name of the event, and el is a reference to
the event listener.
• The method that registers a keyboard event listener is called
addKeyListener(). The method that registers a mouse
motion listener is called addMouseMotionListener().
• When an event occurs, all registered listeners are notified and
receive a copy of the event object.
• A source must also provide a method that allows a listener to
unregister an interest in a specific type of event. The general
form of such a method is this:

public void removeTypeListener(TypeListener el)

• Here, Type is the name of the event, and el is a reference to


the event listener. For example, to remove a keyboard
listener, you would call removeKeyListener( ).
• The methods that add or remove listeners are provided by the
source that generates events.
• For example, the Component class provides methods to add
and remove keyboard and mouse event listeners.
Event Listeners
• A listener is an object that is notified when an event occurs. It has
two major requirements.
• First, it must have been registered with one or more sources
to receive notifications about specific types of events.
• Second, it must implement methods to receive and process
these notifications.
• The methods that receive and process events are defined in
a set of interfaces found in java.awt.event.
• For example, the MouseMotionListener interface defines
methods that receive notifications when the mouse is
dragged or moved.
Event Classes
• The classes that represent events are at the core of Java’s
event handling mechanism.
• At the root of the Java event class hierarchy is EventObject,
which is in java.util. It is the superclass for all events. Its one
constructor is shown here:
EventObject (Object src) – src is a reference to the object
that generates the event.
• EventObject contains two methods: getSource() & toString()
• The getSource() returns the source of event.
Object getSource()
• The toString() returns the string equivalent of the event.
• EventObject is a supreclass of all events.
• AWTEvent, defined within the java.awt package is a subclass
of EventObject.
• AWTEvent is a superclass of all AWT events that are
handled by the delegation event model.
• getID() is a method used to determine the type of the event.
int getID()
Event Class Listener Interface Methods Descriptions
ActionEvent ActionListener actionPerformed() ActionEvent indicates
that a component-
defined action occurred.

AdjustmentE AdjustmentListener adjustmentValueChanged() Adjustment events


vent occur by an adjustable
object like a scrollbar.

ComponentE ComponentListener componentResized(), An event occurs when a


vent componentMoved(), component moved,
componentShown() and changed its visibility or
componentHidden() the size changed.

ContainerEve ContainerListener componentRemoved() and The event is fired when


nt componentAdded() a component is added or
removed from a
container.

FocusEvent FocusListener focusLost() and Focus events include


focusGained() focus, focusout, focusin,
and blur.
Event Class Listener Interface Methods Descriptions
ItemEvent ItemListener itemStateChanged() Item event occurs when
an item is selected.

KeyEvent KeyListener keyPressed(), keyReleased(), A key event occurs when


keyTyped(). the user presses a key on
the keyboard.

MouseEvent MouseListener and mouseListener methods : A mouse event occurs


MouseMotionListe mouseClicked(), when the user interacts
ner mousePressed(), with the mouse.
mouseEntered(),
mouseExited()
mouseReleased()
MouseMotionListener():
mouseDragged()
mouseMoved()

MouseWheel MouseWheelListen mouseWheelMoved(). MouseWheelEvent


Event er occurs when the mouse
wheel rotates in a
component.
Event Class Listener Interface Methods Descriptions
TextEvent TextListener textChanged() TextEvent occurs when
an object's text change.

WindowEven WindowListener windowActivated(), Window events occur


t windowDeactivated(), when a window's status
windowOpened(), is changed.
windowClosed(),
windowClosing(),

You might also like