SlideShare uma empresa Scribd logo
1 de 23
Why Does a Program or Application Need to
be Event Driven?
 Before event handling came into the picture, a program had to collect all the user
information itself to know what it was doing at a given point in time.
 This means that after being run or initialized, a program was always in a big repeat loop that
was waiting for the user to do something.
 So, the program was looking for any action – from the press of a button to slider movement.
After it came to know that something has happened from the user’s side, it prepared itself to
deliver the appropriate response. This is referred to as polling.
 Although polling gets the job done, more often than not, it comes across as too
unmanageable and time-consuming a task.
 If we consider using it for modern-day applications, it doesn’t really fit the
requirements. Two primary reasons make polling unsuitable for modern
applications –
1.Polling puts all the code inside the big repeat loop, and the interactions
that take place inside this location are too complex.
2.Also, polling makes a program enter a never-ending loop, which results in the
exhaustion of CPU cycles without any guarantee of action coming from the user.
 The Abstract Window Toolkit or AWT has gone ahead and struck
association with a different working model for solving the issue discussed
above. This new model is event-driven programming.
.
 With AWT, there is no need for the program to look out for user-
generated events. It is the Java run time that does this job. It
intimates the program as soon as an event takes place. It saves a
valuable resource from exhaustion and handles user interaction
better.
Abstract Window Toolkit (AWT) is a set of application
program interfaces used by Java programmers to create
graphical user interface ( GUI ) objects, such as buttons, scroll
bars, and windows
Event Handling
Event Handling is the mechanism that controls the event &
decides what should happen if an event occurs.
Chain of Responsibility
Delegation Event Model
In the old days, Java used a Chain of Responsibility pattern
to process events.
 For example, when a button is clicked, a event is
generated, which then is passed through a chain of
components.
 The chain of components is defined by the hierarchy of
classes and interfaces. An event is caught and handled
by the handler class.
 This mechanism was used by Java version 1.0, which
this required components to receive events that they did
not process & it wasted valuable time.
 The delegation event modeleliminates
this overhead.(Java version 1.1 onwards)
The Delegation Event Model
The delegation event model defines standard and consistent mechanisms to generate
and process events.
Principle:
 A source generates an event and sends it to one or more listeners.
 The listener waits until it receives an event.
 Once an event is received, the listener processes the event and then returns.
Advantage:
 The application logic that processes events is cleanly separated from the user
interface logic that generates those events.
 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.
Event
Changing the state of an object is known as an event.
They are external effects that are controlled by the user
For example, click on button, dragging mouse etc.
The event object is used to carry the required information about the
state change.
 When we click on the "click me" button an event is generated; that
change event generates another frame that shows our message, that
is passed to the program.
 When you press a button in your program or Android
application the state of the button changes from ‘Unclicked’
to ‘Clicked’. This change in the state of our button is called
an Event.
Types of Event
1. 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.
Foreground Events
Background Events
2.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 Sources
A source is an object that generates an event
Event Sources are responsible for generating events and are
called components.
It also provides information about the event to the listener
Event sources could be anything from text boxes and combo
boxes to buttons, and more
Sources may generate more than one type of event
Listeners
 A listener is an object that listens to the event. A listener gets
notified when an event occurs
Events are handled by a special group of interfaces, known as
"listeners".
Listeners are also called as event handlers as they are the ones
responsible to handle events occurring at the source.
 Listeners are interfaces and different types of listeners are used
according to the event
 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.
Here is the general form
public void addTypeListener(TypeListener el)
For example:
Button b=new Button("click me");
b.addActionListener(this);
 Here, type is the name of the event, and el is a reference to the
event listener.
 For example, 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. This is known as multicasting the event.
 In all cases, notifications are sent only to listeners that register to
receive them
 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 an object that is notified when an event listener. For
example, to remove a keyboard listener, you would call
removeKeyListener()
Important Event Classes and Interface
EVENT CLASSES DESCRIPTION LISTENER INTERFACE
ActionEvent generated when button is
pressed, menu-item is selected,
list-item is double clicked
ActionListener
MouseEvent generated when mouse is
dragged,
moved,clicked,pressed or
released and also when it
enters or exit a component
MouseListener
KeyEvent generated when input is
received from keyboard
KeyListener
ItemEvent generated when check-box or
list item is clicked
ItemListener
TextEvent generated when value of
textarea or textfield is changed
TextListener
MouseWheelEvent generated when mouse wheel
is moved
MouseWheelListener
WindowEvent generated when window is
activated, deactivated,
deiconified, iconified,
opened or closed
WindowListener
ComponentEvent generated when
component is hidden,
moved, resized or set
visible
ComponentListener
ContainerEvent generated when
component is added or
removed from container
ContainerListener
AdjustmentEvent generated when scroll bar
is manipulated
AdjustmentListener
FocusEvent generated when
component gains or loses
keyboard focus
FocusListener
EVENT CLASSES EVENTS LISTENER INTERFACE
ActionEvent Button
MenuItem
List
ActionListener
AdjustmentEvent Component AdjustmentListener
ComponentEvent Component ComponentListener
ContainerEvent Component ContainerListener
How Events are handled ?
 A source generates an Event and send it to one or more listeners
registered with the source. Once event is received by the listener,
they process the event and then return.
 Each type of event has its own registration method
Steps to handle events:
 Implement appropriate interface in the class.
 Register the component with the listener
Registration Methods
For registering the component with the Listener, many classes
provide the registration methods
For example:
Button
public void addActionListener(ActionListener a){}
MenuItem
public void addActionListener(ActionListener a){}
TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
TextArea
public void addTextListener(TextListener a){}
Checkbox
public void addItemListener(ItemListener a){}
Choice
public void addItemListener(ItemListener a){}
List
public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}
LISTENER INTERFACE METHODS
ActionListener actionPerformed()
AdjustmentListener adjustmentValueChanged()
ComponentListener componentResized()
componentMoved()
componentShown()
componentHidden()
ContainerListener componentAdded()
componentRemoved()

Mais conteúdo relacionado

Mais procurados

Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statementnarmadhakin
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer Swati Kulkarni Jaipurkar
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception HandlingMegha V
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threadingAPU
 
Java collections concept
Java collections conceptJava collections concept
Java collections conceptkumar gaurav
 
Command line arguments
Command line argumentsCommand line arguments
Command line argumentsAshok Raj
 
Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.mohanrathod18
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
 

Mais procurados (20)

Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Java thread life cycle
Java thread life cycleJava thread life cycle
Java thread life cycle
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
 
Java input
Java inputJava input
Java input
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Input output streams
Input output streamsInput output streams
Input output streams
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Java IO
Java IOJava IO
Java IO
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threading
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.
 
Web controls
Web controlsWeb controls
Web controls
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 

Semelhante a Event handling in Java(part 1)

Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in JavaAyesha Kanwal
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVASrajan Shukla
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03Ankit Dubey
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingPayal Dungarwal
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handlingteach4uin
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxusvirat1805
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxudithaisur
 
Mobile Application Development
Mobile Application DevelopmentMobile Application Development
Mobile Application DevelopmentMuhammad Sajid
 
Chap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxTadeseBeyene
 
Java gui event
Java gui eventJava gui event
Java gui eventSoftNutx
 
Event+driven+programming key+features
Event+driven+programming key+featuresEvent+driven+programming key+features
Event+driven+programming key+featuresFaisal Aziz
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)jammiashok123
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptsharanyak0721
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handlingAmol Gaikwad
 

Semelhante a Event handling in Java(part 1) (20)

Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
What is Event
What is EventWhat is Event
What is Event
 
Unit-1.pptx
Unit-1.pptxUnit-1.pptx
Unit-1.pptx
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
 
Mobile Application Development
Mobile Application DevelopmentMobile Application Development
Mobile Application Development
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
 
Chap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptx
 
Java gui event
Java gui eventJava gui event
Java gui event
 
Event+driven+programming key+features
Event+driven+programming key+featuresEvent+driven+programming key+features
Event+driven+programming key+features
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 
Events vs Notifications
Events vs NotificationsEvents vs Notifications
Events vs Notifications
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 

Último

Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 

Último (20)

Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 

Event handling in Java(part 1)

  • 1.
  • 2. Why Does a Program or Application Need to be Event Driven?  Before event handling came into the picture, a program had to collect all the user information itself to know what it was doing at a given point in time.  This means that after being run or initialized, a program was always in a big repeat loop that was waiting for the user to do something.  So, the program was looking for any action – from the press of a button to slider movement. After it came to know that something has happened from the user’s side, it prepared itself to deliver the appropriate response. This is referred to as polling.  Although polling gets the job done, more often than not, it comes across as too unmanageable and time-consuming a task.
  • 3.  If we consider using it for modern-day applications, it doesn’t really fit the requirements. Two primary reasons make polling unsuitable for modern applications – 1.Polling puts all the code inside the big repeat loop, and the interactions that take place inside this location are too complex. 2.Also, polling makes a program enter a never-ending loop, which results in the exhaustion of CPU cycles without any guarantee of action coming from the user.  The Abstract Window Toolkit or AWT has gone ahead and struck association with a different working model for solving the issue discussed above. This new model is event-driven programming. .
  • 4.  With AWT, there is no need for the program to look out for user- generated events. It is the Java run time that does this job. It intimates the program as soon as an event takes place. It saves a valuable resource from exhaustion and handles user interaction better. Abstract Window Toolkit (AWT) is a set of application program interfaces used by Java programmers to create graphical user interface ( GUI ) objects, such as buttons, scroll bars, and windows
  • 5. Event Handling Event Handling is the mechanism that controls the event & decides what should happen if an event occurs. Chain of Responsibility Delegation Event Model
  • 6. In the old days, Java used a Chain of Responsibility pattern to process events.  For example, when a button is clicked, a event is generated, which then is passed through a chain of components.  The chain of components is defined by the hierarchy of classes and interfaces. An event is caught and handled by the handler class.  This mechanism was used by Java version 1.0, which this required components to receive events that they did not process & it wasted valuable time.  The delegation event modeleliminates this overhead.(Java version 1.1 onwards)
  • 7. The Delegation Event Model The delegation event model defines standard and consistent mechanisms to generate and process events. Principle:  A source generates an event and sends it to one or more listeners.  The listener waits until it receives an event.  Once an event is received, the listener processes the event and then returns. Advantage:  The application logic that processes events is cleanly separated from the user interface logic that generates those events.  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.
  • 8. Event Changing the state of an object is known as an event. They are external effects that are controlled by the user For example, click on button, dragging mouse etc. The event object is used to carry the required information about the state change.  When we click on the "click me" button an event is generated; that change event generates another frame that shows our message, that is passed to the program.
  • 9.  When you press a button in your program or Android application the state of the button changes from ‘Unclicked’ to ‘Clicked’. This change in the state of our button is called an Event.
  • 10. Types of Event 1. 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. Foreground Events Background Events
  • 11. 2.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.
  • 12. Event Sources A source is an object that generates an event Event Sources are responsible for generating events and are called components. It also provides information about the event to the listener Event sources could be anything from text boxes and combo boxes to buttons, and more Sources may generate more than one type of event
  • 13.
  • 14. Listeners  A listener is an object that listens to the event. A listener gets notified when an event occurs Events are handled by a special group of interfaces, known as "listeners". Listeners are also called as event handlers as they are the ones responsible to handle events occurring at the source.  Listeners are interfaces and different types of listeners are used according to the event  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.
  • 15. Here is the general form public void addTypeListener(TypeListener el) For example: Button b=new Button("click me"); b.addActionListener(this);  Here, type is the name of the event, and el is a reference to the event listener.  For example, 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. This is known as multicasting the event.  In all cases, notifications are sent only to listeners that register to receive them
  • 16.  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 an object that is notified when an event listener. For example, to remove a keyboard listener, you would call removeKeyListener()
  • 17. Important Event Classes and Interface EVENT CLASSES DESCRIPTION LISTENER INTERFACE ActionEvent generated when button is pressed, menu-item is selected, list-item is double clicked ActionListener MouseEvent generated when mouse is dragged, moved,clicked,pressed or released and also when it enters or exit a component MouseListener KeyEvent generated when input is received from keyboard KeyListener ItemEvent generated when check-box or list item is clicked ItemListener TextEvent generated when value of textarea or textfield is changed TextListener MouseWheelEvent generated when mouse wheel is moved MouseWheelListener
  • 18. WindowEvent generated when window is activated, deactivated, deiconified, iconified, opened or closed WindowListener ComponentEvent generated when component is hidden, moved, resized or set visible ComponentListener ContainerEvent generated when component is added or removed from container ContainerListener AdjustmentEvent generated when scroll bar is manipulated AdjustmentListener FocusEvent generated when component gains or loses keyboard focus FocusListener
  • 19. EVENT CLASSES EVENTS LISTENER INTERFACE ActionEvent Button MenuItem List ActionListener AdjustmentEvent Component AdjustmentListener ComponentEvent Component ComponentListener ContainerEvent Component ContainerListener
  • 20. How Events are handled ?  A source generates an Event and send it to one or more listeners registered with the source. Once event is received by the listener, they process the event and then return.  Each type of event has its own registration method
  • 21. Steps to handle events:  Implement appropriate interface in the class.  Register the component with the listener Registration Methods For registering the component with the Listener, many classes provide the registration methods
  • 22. For example: Button public void addActionListener(ActionListener a){} MenuItem public void addActionListener(ActionListener a){} TextField public void addActionListener(ActionListener a){} public void addTextListener(TextListener a){} TextArea public void addTextListener(TextListener a){} Checkbox public void addItemListener(ItemListener a){} Choice public void addItemListener(ItemListener a){} List public void addActionListener(ActionListener a){} public void addItemListener(ItemListener a){}
  • 23. LISTENER INTERFACE METHODS ActionListener actionPerformed() AdjustmentListener adjustmentValueChanged() ComponentListener componentResized() componentMoved() componentShown() componentHidden() ContainerListener componentAdded() componentRemoved()