SlideShare a Scribd company logo
Event Handling in Java
Event :
An event is an Object that describes a
state change in a Source.
* Some of the activities that causes event
to be generated are :
*Pressing a Button.
*Entering a character through
Key Board.
*Selecting an item form a list etc.
Event Handling in Java
Event Handling in Java
 For implementing event listener we have to
import the following Statement:
import java.awt.event.*;
 Event Handling is the mechanism that
controls the event and decides what should
happen if an event occurs.This mechanism
have the code which is known as event
handler that is executed when an event
occurs. Java Uses the Delegation Event
Model to handle the events.This model
defines the standard mechanism to
generate and handle the events
 Foreground Events:
Those events which require
the direct interaction of user.They are
generated as consequences of a person
interacting with the graphical components in
Graphical User Interface. For example, clicking
on a button, moving the mouse, entering a
character through keyboard,selecting an item
from list, scrolling the page etc.
 Background Events :
Those events that require
the interaction of end user are known as
background events. Operating system
interrupts, hardware or software failure,
timer expires, an operation completion are
the example of background events.
Event Handling in Java
 Input events:
 Input events generally carry three pieces of
information: the event type, the location of
the mouse or pen when the event occurred,
and some modifier button information.The
modifier information includes which
mouse/pen buttons are currently pressed
and whether control, shift, alt or command
keys are also pressed at the time.
 Key events:
 The component peers deliver separate key
events when a user presses and releases
nearly any key. KEY_ACTION and
KEY_ACTION_RELEASE are for the
function and arrow keys, while KEY_PRESS
and KEY_RELEASE are for the remaining
control and alphanumeric keys.
 Mouse events
 The component peers deliver mouse events
when a user presses or releases a mouse
button. Events are also delivered whenever
the mouse moves.
 For example :
 MouseMotionListener interface
define two events:
 When mouse is dragged.
 When mouse is moved.
 Focus events
 The peers deliver focus events when a
component gains (GOT_FOCUS) or
loses(LOST_FOCUS) the input focus. No
default methods are called for the focus
events. They must be dealt with in the
handleEvent() method of the Container of
the component or a subclass of the
component.
 ActionEvent
 The ActionEvent class is the first higher-
level event class. It encapsulates events
that signify that the user is doing
something with a component.When the
user selects a button, list item, or menu
item, or presses the Return key in a text
field, an ActionEvent passes through the
event queue looking for listeners.
 ItemEvent:
 The ItemEvent class is another higher-level
event class. It encapsulates events that occur
when the user selects a component, like
ActionEvent. When the user selects a
checkbox, choice, list item, or checkbox menu
item, an ItemEvent passes through the event
queue looking for listeners. Although there is
only one type of ItemEvent, there are two
subtypes represented by the constants
SELECTED and DE- SELECTED.
 It is simple and well suited to an object-
oriented programming environment.
► An event can only be handled by the
component from which it originated or by
one of the containers of the originating
component.
►In order to handle events, you must either
subclass the component that receives the
event or create a handleEvent() method at
the base container.
 A delegate is better adapted to an object-
oriented language model and is completely
type checked for code reliability.
 Source -The source is an object on which
event occurs. Source is responsible for
providing information of the occurred
event to it's handler. Java provide as with
classes for source object.
 Listener - It is also known as event handler.
Listener is responsible for generating
response to an event.The listener is also an
object. Listener waits until it receives an
event. Once the event is received , the
listener process the event an then returns.
 The User clicks the button and the event is
generated.
 Now the object of concerned event class is
created automatically and information
about the source and the event get
populated with in same object.
 Event object is forwarded to the method of
registered listener class.
 the method is now get executed and
returns.
 In order to design a listener class we have
to develop some listener interfaces.These
Listener interfaces forecast some public
abstract callback methods which must be
implemented by the listener class.
 If you do not implement the any if the
predefined interfaces then your class can
not act as a listener class for a source
object.
 The callback methods represents an event
method. In response to an event java jre
will fire callback method. All such callback
methods are provided in listener interfaces.
Why use Adapter classes?
 Implementing all methods of an interface
takes a lot of work
 Interested in implementing some methods
of the interface only.
 Built-in in Java
 Implement all methods of each listener
interface
 with more than one method.
 Making Simple Calculators By Using Event
Handling:
Event Handling in Java

More Related Content

PPTX
Ado.Net Tutorial
PPTX
Event In JavaScript
PPTX
Packages in java
PDF
Asp.net state management
PPTX
Method overloading
PPTX
Operators and expressions in C++
PPTX
Asp.NET Validation controls
Ado.Net Tutorial
Event In JavaScript
Packages in java
Asp.net state management
Method overloading
Operators and expressions in C++
Asp.NET Validation controls

What's hot (20)

PPTX
Windowforms controls c#
PPTX
String, string builder, string buffer
PPT
Introduction to JavaScript (1).ppt
PPTX
20.3 Java encapsulation
PPTX
MULTI THREADING IN JAVA
PPT
Java: GUI
PPTX
Classes objects in java
PPT
Java layoutmanager
PPT
SQLITE Android
PPTX
Interface in java
PDF
PDF
PPT
Types of exceptions
PPT
Introduction to Javascript
PPT
Jdbc ppt
PPTX
Java input
PPTX
Event Handling in JAVA
PPT
Exception Handling in JAVA
PPT
Servlet life cycle
Windowforms controls c#
String, string builder, string buffer
Introduction to JavaScript (1).ppt
20.3 Java encapsulation
MULTI THREADING IN JAVA
Java: GUI
Classes objects in java
Java layoutmanager
SQLITE Android
Interface in java
Types of exceptions
Introduction to Javascript
Jdbc ppt
Java input
Event Handling in JAVA
Exception Handling in JAVA
Servlet life cycle
Ad

Viewers also liked (19)

PPTX
Java programming-Event Handling
PPTX
Event Handling in java
PPTX
tL20 event handling
PPT
Java Event Handling
PDF
Basic Event Listener
PPTX
Event handling
PPT
๋Java Programming [7/12] : GUI Event Handling
PPT
Learning Java 4 – Swing, SQL, and Security API
PPTX
Java 8 parallel stream
PPS
Java session11
PDF
Concurrency Utilities in Java 8
PDF
Java 8 Stream API. A different way to process collections.
PPT
Learning Java 3 – Threads and Synchronization
PDF
Working With Concurrency In Java 8
PDF
Java Thread Synchronization
PPT
Java 8 Streams
PPT
Java awt
PPTX
PPTX
Java programming-Event Handling
Event Handling in java
tL20 event handling
Java Event Handling
Basic Event Listener
Event handling
๋Java Programming [7/12] : GUI Event Handling
Learning Java 4 – Swing, SQL, and Security API
Java 8 parallel stream
Java session11
Concurrency Utilities in Java 8
Java 8 Stream API. A different way to process collections.
Learning Java 3 – Threads and Synchronization
Working With Concurrency In Java 8
Java Thread Synchronization
Java 8 Streams
Java awt
Ad

Similar to Event Handling in Java (20)

PPTX
What is Event
PPT
unit-6.pptbjjdjdkd ndmdkjdjdjjdkfjjfjfjfj
PPTX
Advance java programming- Event handling
PPTX
Advance Java Programming(CM5I) Event handling
PPTX
ITE 1122_ Event Handling.pptx
PPT
Chapter 8 event Handling.ppt m m m m m m
PPTX
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
PPTX
event-handling.pptx
PDF
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
PPT
Java gui event
PDF
java-programming GUI- Event Handling.pdf
PPTX
UNIT III Event Handling of the car j.pptx
PDF
Java Programming :Event Handling(Types of Events)
PPTX
EVENT HANDLING in Object Oriented Programming in Java
PPTX
Module3.11.pptx
PPT
Unit 6 Java
PDF
Event handling
PDF
Event handling
PPTX
Event handling
PPTX
Module 5.pptx
What is Event
unit-6.pptbjjdjdkd ndmdkjdjdjjdkfjjfjfjfj
Advance java programming- Event handling
Advance Java Programming(CM5I) Event handling
ITE 1122_ Event Handling.pptx
Chapter 8 event Handling.ppt m m m m m m
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
event-handling.pptx
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Java gui event
java-programming GUI- Event Handling.pdf
UNIT III Event Handling of the car j.pptx
Java Programming :Event Handling(Types of Events)
EVENT HANDLING in Object Oriented Programming in Java
Module3.11.pptx
Unit 6 Java
Event handling
Event handling
Event handling
Module 5.pptx

Recently uploaded (20)

PPTX
Introduction to Artificial Intelligence
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
System and Network Administraation Chapter 3
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Essential Infomation Tech presentation.pptx
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
top salesforce developer skills in 2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
history of c programming in notes for students .pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Transform Your Business with a Software ERP System
PDF
medical staffing services at VALiNTRY
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Introduction to Artificial Intelligence
Softaken Excel to vCard Converter Software.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
System and Network Administraation Chapter 3
VVF-Customer-Presentation2025-Ver1.9.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Navsoft: AI-Powered Business Solutions & Custom Software Development
Understanding Forklifts - TECH EHS Solution
Essential Infomation Tech presentation.pptx
Upgrade and Innovation Strategies for SAP ERP Customers
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
top salesforce developer skills in 2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Which alternative to Crystal Reports is best for small or large businesses.pdf
history of c programming in notes for students .pptx
CHAPTER 2 - PM Management and IT Context
Transform Your Business with a Software ERP System
medical staffing services at VALiNTRY
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool

Event Handling in Java

  • 2. Event : An event is an Object that describes a state change in a Source. * Some of the activities that causes event to be generated are : *Pressing a Button. *Entering a character through Key Board. *Selecting an item form a list etc.
  • 5.  For implementing event listener we have to import the following Statement: import java.awt.event.*;
  • 6.  Event Handling is the mechanism that controls the event and decides what should happen if an event occurs.This mechanism have the code which is known as event handler that is executed when an event occurs. Java Uses the Delegation Event Model to handle the events.This model defines the standard mechanism to generate and handle the events
  • 7.  Foreground Events: Those events which require the direct interaction of user.They are generated as consequences of a person interacting with the graphical components in Graphical User Interface. For example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item from list, scrolling the page etc.
  • 8.  Background Events : Those events that require the interaction of end user are known as background events. Operating system interrupts, hardware or software failure, timer expires, an operation completion are the example of background events.
  • 10.  Input events:  Input events generally carry three pieces of information: the event type, the location of the mouse or pen when the event occurred, and some modifier button information.The modifier information includes which mouse/pen buttons are currently pressed and whether control, shift, alt or command keys are also pressed at the time.
  • 11.  Key events:  The component peers deliver separate key events when a user presses and releases nearly any key. KEY_ACTION and KEY_ACTION_RELEASE are for the function and arrow keys, while KEY_PRESS and KEY_RELEASE are for the remaining control and alphanumeric keys.
  • 12.  Mouse events  The component peers deliver mouse events when a user presses or releases a mouse button. Events are also delivered whenever the mouse moves.
  • 13.  For example :  MouseMotionListener interface define two events:  When mouse is dragged.  When mouse is moved.
  • 14.  Focus events  The peers deliver focus events when a component gains (GOT_FOCUS) or loses(LOST_FOCUS) the input focus. No default methods are called for the focus events. They must be dealt with in the handleEvent() method of the Container of the component or a subclass of the component.
  • 15.  ActionEvent  The ActionEvent class is the first higher- level event class. It encapsulates events that signify that the user is doing something with a component.When the user selects a button, list item, or menu item, or presses the Return key in a text field, an ActionEvent passes through the event queue looking for listeners.
  • 16.  ItemEvent:  The ItemEvent class is another higher-level event class. It encapsulates events that occur when the user selects a component, like ActionEvent. When the user selects a checkbox, choice, list item, or checkbox menu item, an ItemEvent passes through the event queue looking for listeners. Although there is only one type of ItemEvent, there are two subtypes represented by the constants SELECTED and DE- SELECTED.
  • 17.  It is simple and well suited to an object- oriented programming environment.
  • 18. ► An event can only be handled by the component from which it originated or by one of the containers of the originating component. ►In order to handle events, you must either subclass the component that receives the event or create a handleEvent() method at the base container.
  • 19.  A delegate is better adapted to an object- oriented language model and is completely type checked for code reliability.
  • 20.  Source -The source is an object on which event occurs. Source is responsible for providing information of the occurred event to it's handler. Java provide as with classes for source object.
  • 21.  Listener - It is also known as event handler. Listener is responsible for generating response to an event.The listener is also an object. Listener waits until it receives an event. Once the event is received , the listener process the event an then returns.
  • 22.  The User clicks the button and the event is generated.  Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object.  Event object is forwarded to the method of registered listener class.  the method is now get executed and returns.
  • 23.  In order to design a listener class we have to develop some listener interfaces.These Listener interfaces forecast some public abstract callback methods which must be implemented by the listener class.  If you do not implement the any if the predefined interfaces then your class can not act as a listener class for a source object.
  • 24.  The callback methods represents an event method. In response to an event java jre will fire callback method. All such callback methods are provided in listener interfaces.
  • 25. Why use Adapter classes?  Implementing all methods of an interface takes a lot of work  Interested in implementing some methods of the interface only.
  • 26.  Built-in in Java  Implement all methods of each listener interface  with more than one method.  Making Simple Calculators By Using Event Handling: