SlideShare a Scribd company logo
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()

More Related Content

What's hot

Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
Gurpreet singh
 
Ado.Net Tutorial
Ado.Net TutorialAdo.Net Tutorial
Ado.Net Tutorial
prabhu rajendran
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
Vikas Jagtap
 
Java awt
Java awtJava awt
Java awt
Arati Gadgil
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Event handling
Event handlingEvent handling
Layout manager
Layout managerLayout manager
Java media framework
Java media frameworkJava media framework
Java media framework
Payal Vishwakarma
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
sandeep54552
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Kasun Madusanke
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Object diagram
Object diagramObject diagram
Object diagram
Preeti Mishra
 
C# Framework class library
C# Framework class libraryC# Framework class library
C# Framework class library
Prem Kumar Badri
 
Introduction to Eclipse IDE
Introduction to Eclipse IDEIntroduction to Eclipse IDE
Introduction to Eclipse IDE
Muhammad Hafiz Hasan
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Files in php
Files in phpFiles in php
Files in php
sana mateen
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
kamal kotecha
 
Validation Controls in asp.net
Validation Controls in asp.netValidation Controls in asp.net
Validation Controls in asp.net
Deep Patel
 

What's hot (20)

Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
 
Ado.Net Tutorial
Ado.Net TutorialAdo.Net Tutorial
Ado.Net Tutorial
 
Event handling
Event handlingEvent handling
Event handling
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Java awt
Java awtJava awt
Java awt
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Event handling
Event handlingEvent handling
Event handling
 
Layout manager
Layout managerLayout manager
Layout manager
 
Java media framework
Java media frameworkJava media framework
Java media framework
 
Uml class-diagram
Uml class-diagramUml class-diagram
Uml class-diagram
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Object diagram
Object diagramObject diagram
Object diagram
 
C# Framework class library
C# Framework class libraryC# Framework class library
C# Framework class library
 
Introduction to Eclipse IDE
Introduction to Eclipse IDEIntroduction to Eclipse IDE
Introduction to Eclipse IDE
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Files in php
Files in phpFiles in php
Files in php
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
Validation Controls in asp.net
Validation Controls in asp.netValidation Controls in asp.net
Validation Controls in asp.net
 

Similar to Event handling in Java(part 1)

Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
Ayesha Kanwal
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
Srajan Shukla
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
Ankit Dubey
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
Payal Dungarwal
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
simmis5
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
teach4uin
 
Unit-1.pptx
Unit-1.pptxUnit-1.pptx
Unit-1.pptx
VikasTuwar1
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
usvirat1805
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
udithaisur
 
Mobile Application Development
Mobile Application DevelopmentMobile Application Development
Mobile Application Development
Muhammad Sajid
 
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
Jyothishmathi Institute of Technology and Science Karimnagar
 
Chap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptx
TadeseBeyene
 
Java gui event
Java gui eventJava gui event
Java gui event
SoftNutx
 
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
 
Events vs Notifications
Events vs NotificationsEvents vs Notifications
Events vs Notifications
jeetendra mandal
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
sharanyak0721
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
Amol Gaikwad
 

Similar to 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
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
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
 

Recently uploaded

Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 

Recently uploaded (20)

Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.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()