SlideShare una empresa de 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()

Más contenido relacionado

La actualidad más candente (20)

Namespaces in C#
Namespaces in C#Namespaces in C#
Namespaces in C#
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Java-java virtual machine
Java-java virtual machineJava-java virtual machine
Java-java virtual machine
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Python datetime
Python datetimePython datetime
Python datetime
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Event handling
Event handlingEvent handling
Event handling
 
Java awt
Java awtJava awt
Java awt
 
Java swing
Java swingJava swing
Java swing
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Java applet
Java appletJava applet
Java applet
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Event+driven+programming key+features
Event+driven+programming key+featuresEvent+driven+programming key+features
Event+driven+programming key+features
 

Similar a Event handling in Java(part 1)

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
 
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 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
 
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
 
Discuss how each of the following Java components will assist in creat.docx
Discuss how each of the following Java components will assist in creat.docxDiscuss how each of the following Java components will assist in creat.docx
Discuss how each of the following Java components will assist in creat.docxwviola
 

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

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
 
Event handling
Event handlingEvent handling
Event handling
 
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
 
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
 
Discuss how each of the following Java components will assist in creat.docx
Discuss how each of the following Java components will assist in creat.docxDiscuss how each of the following Java components will assist in creat.docx
Discuss how each of the following Java components will assist in creat.docx
 
event_handling.ppt
event_handling.pptevent_handling.ppt
event_handling.ppt
 

Último

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 

Último (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.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()