SlideShare a Scribd company logo
1 of 22
Event handling




                 http://improvejava.blogspot.in/
                                                   1
Objectives

On completion of this period, you would be
able to know
• Event handling
• Delegation event model
• Events
• Event Sources
• Event Listeners



              http://improvejava.blogspot.in/
                                                2
Recap

In the previous class, you have leant
• The AWT classes




                http://improvejava.blogspot.in/
                                                  3
Event Handling

• The AWT based programs and applets are
  event-driven programs
• Event handling is at the core of success of these
  programs
• The modern approach to handling events is
  based on the ‘delegation event model’




                  http://improvejava.blogspot.in/   4
The Delegation Event Model

• This model defines
   – standard and consistent mechanisms to generate and
     process events
• Its concept is
   – A source generates an event and sends it to one or
     more listeners.
   – The listener simply waits until it receives an event
   – Once received, the listener processes the event and
     then returns
   – Listeners must register with a source in order to
     receive an event notification

                    http://improvejava.blogspot.in/         5
The Delegation Event Model                      contd..


• The advantages of this model are
  – The application logic that processes events is
    cleanly separated from the user interface logic
  – Notifications are sent only to listeners that
    want to receive them




                 http://improvejava.blogspot.in/             6
Events
• An event is an object that describes a state
  change in a source
• It can be generated as a consequence of a
  person interacting with the elements in a GUI
• Examples include
  –   pressing a button
  –   entering a character via the keyboard
  –   selecting an item in a list
  –   clicking the mouse


                     http://improvejava.blogspot.in/   7
Event Classes

• The classes that represent events are at the
  core of Java’s event handling mechanism
• EventObject
  – Defined in java.util package
  – It is the superclass for all events
• AWTEvent
  – Defined within the java.awt package
  – It is a subclass of EventObject
  – It is a superclass of all AWT events that are handled
    by the delegation event model

                     http://improvejava.blogspot.in/    8
Event Classes                          contd..
• The package java.awt.event defines several types of
  events that are generated by various user interface
  elements
     Event Name                          Generated when
  ActionEvent       A button is pressed, a list item is
                    double-clicked, or a menu item is selected
  AdjustmentEvent   A scroll bar is manipulated

  ComponentEvent    A component is hidden, moved, resized, or
                    becomes visible
  ContainerEvent    A component is added to or removed from a
                    container
  FocusEvent        A component gains or loses keyboard focus

                      Table 62.1 Event classes
                     http://improvejava.blogspot.in/             9
Event Classes                          contd..

InputEvent       Abstract super class for all component input event
                 classes
ItemEvent        A check box or list item is clicked; also occurs when
                 a choice selection is made or a checkable menu
                 item is selected or deselected
KeyEvent         Input is received from the keyboard
MouseEvent       The mouse is dragged, moved, clicked, pressed, or
                 released; also generated when the mouse enters
                 or exits a component
TextEvent        The value of a text area or text field is Changed

WindowEvent      Window is activated, closed, deactivated,
                 deiconified, iconified, opened, or quit
                 Table 62.2 Event classes
                   http://improvejava.blogspot.in/                       10
Event Sources
• A source is an object that generates an event
• e.g.
  – A button
  – A TextField
• Sources may generate more than one type of
  event
• e.g .
  – Click on button
  – Double click on button


                   http://improvejava.blogspot.in/   11
Event Sources                               contd..

• A source must register listeners in order for the listeners
  to receive notifications about a specific type of event
• Each type of event has its own registration method
• the general form:
   – public void addTypeListener(TypeListener el)
   – Here, Type is the name of the event and el is a reference to the
     event listener.
   – e.g.,
   – the method that registers a keyboard event listener is called
     addKeyListener( )
   – The method that registers a mouse motion listener is called
     addMouseMotionListener( )


                        http://improvejava.blogspot.in/             12
Event Sources                              contd..


• When an event occurs
  – All registered listeners are
     • Notified about the event
     • As well as, receive a copy of the event object
• This is known as multicasting the event




                   http://improvejava.blogspot.in/             13
Event Sources                              contd..
Event Source      Generates
Button            action events when the button is pressed

Checkbox          item events when the check box is selected or deselected

Choice            item events when the choice is changed
List              action events when an item is double-clicked; generates
                  item events when an item is selected or deselected
Menu Item         action events when a menu item is selected;
                  Item events when a checkable menu item is selected or
                  deselected
Scrollbar         adjustment events when the scroll bar is manipulated
Text components   text events when the user enters a character
Window            window events when a window is activated, closed,
                  deactivated, deiconified, iconified, opened, or quit.
                         Table 62.3 Event sources
                        http://improvejava.blogspot.in/                     14
Event Listeners

• A listener is an object that is notified when
  an event occurs
• It has two major requirements
  – it must have been registered with one or more
    sources to receive notifications
  – it must implement methods to receive and
    process these notifications



                http://improvejava.blogspot.in/   15
Event Listeners                          contd..


• Listeners are created by implementing one or
  more of the interfaces
• When an event occurs, the event source invokes
  the appropriate method defined by the listener
  and provides an event object as its argument




                http://improvejava.blogspot.in/             16
Event Listeners                           contd..
      Interface                                   Description
ActionListener        Defines one method to receive action events

AdjustmentListener    Defines one method to receive adjustment events

ComponentListener     Defines four methods to recognize when a
                      component is hidden, moved, resized, or shown
ContainerListener     Defines two methods to recognize when a component is
                      added to or removed from a Container
FocusListener         Defines two methods to recognize when a component
                      gains or loses keyboard focus
ItemListener          Defines one method to recognize when the state of an
                      item changes
KeyListener           Defines three methods to recognize when a key is
                      pressed, released, or typed

                      Table 62.4 Event Listeners
                         http://improvejava.blogspot.in/                 17
Event Listeners                             contd..
      Interface                                   Description
MouseListener         Defines five methods to recognize when the mouse is
                      clicked, enters a component, exits a component, is
                      pressed, or is released
MouseMotionListener   Defines two methods to recognize when the mouse is
                      dragged or moved
TextListener          Defines one method to recognize when a text value
                      Changes
WindowListener        Defines seven methods to recognize when a window is
                      activated, closed, deactivated, deiconified, iconified,
                      opened, or quit

                      Table 62.5 Event Listeners




                         http://improvejava.blogspot.in/                   18
Summary

• In this class we discussed
  – Event handling
  – Delegation event model
  – Events
  – Event Sources
  – Event Listeners




               http://improvejava.blogspot.in/   19
Quiz

1. What is the event source for AdjustmentEvent ?
  a)   TextField
  b)   List
  c)   ScrollBar
  d)   Button




                   http://improvejava.blogspot.in/   20
Quiz                    contd..

2. EventObject is defined in which package ?
  a)   java.awt
  b)   java.awt.event
  c)   java.util
  d)   java.applet




                    http://improvejava.blogspot.in/             21
Frequently Asked Questions

1. Explain the delegation model of event handling
2. List some example for event sources
3. What is event listeners? List some examples
   for event listener




                 http://improvejava.blogspot.in/   22

More Related Content

Similar to Event handling62

Java event processing model in c# and java
Java  event processing model in c# and javaJava  event processing model in c# and java
Java event processing model in c# and java
Tech_MX
 

Similar to Event handling62 (20)

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
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
Event handling
Event handlingEvent handling
Event handling
 
Event handling
Event handlingEvent handling
Event handling
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
javabeans
javabeansjavabeans
javabeans
 
Module3.11.pptx
Module3.11.pptxModule3.11.pptx
Module3.11.pptx
 
Module 5.pptx
Module 5.pptxModule 5.pptx
Module 5.pptx
 
Event handling in Java(part 1)
Event handling in Java(part 1)Event handling in Java(part 1)
Event handling in Java(part 1)
 
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)
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intent
 
Dacj 2-2 b
Dacj 2-2 bDacj 2-2 b
Dacj 2-2 b
 
File Handling
File HandlingFile Handling
File Handling
 
OOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxOOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptx
 
Chap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptx
 
Java event processing model in c# and java
Java  event processing model in c# and javaJava  event processing model in c# and java
Java event processing model in c# and java
 
EventHandling.pptx
EventHandling.pptxEventHandling.pptx
EventHandling.pptx
 

More from myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Event handling62

  • 1. Event handling http://improvejava.blogspot.in/ 1
  • 2. Objectives On completion of this period, you would be able to know • Event handling • Delegation event model • Events • Event Sources • Event Listeners http://improvejava.blogspot.in/ 2
  • 3. Recap In the previous class, you have leant • The AWT classes http://improvejava.blogspot.in/ 3
  • 4. Event Handling • The AWT based programs and applets are event-driven programs • Event handling is at the core of success of these programs • The modern approach to handling events is based on the ‘delegation event model’ http://improvejava.blogspot.in/ 4
  • 5. The Delegation Event Model • This model defines – standard and consistent mechanisms to generate and process events • Its concept is – A source generates an event and sends it to one or more listeners. – The listener simply waits until it receives an event – Once received, the listener processes the event and then returns – Listeners must register with a source in order to receive an event notification http://improvejava.blogspot.in/ 5
  • 6. The Delegation Event Model contd.. • The advantages of this model are – The application logic that processes events is cleanly separated from the user interface logic – Notifications are sent only to listeners that want to receive them http://improvejava.blogspot.in/ 6
  • 7. Events • An event is an object that describes a state change in a source • It can be generated as a consequence of a person interacting with the elements in a GUI • Examples include – pressing a button – entering a character via the keyboard – selecting an item in a list – clicking the mouse http://improvejava.blogspot.in/ 7
  • 8. Event Classes • The classes that represent events are at the core of Java’s event handling mechanism • EventObject – Defined in java.util package – It is the superclass for all events • AWTEvent – Defined within the java.awt package – It is a subclass of EventObject – It is a superclass of all AWT events that are handled by the delegation event model http://improvejava.blogspot.in/ 8
  • 9. Event Classes contd.. • The package java.awt.event defines several types of events that are generated by various user interface elements Event Name Generated when ActionEvent A button is pressed, a list item is double-clicked, or a menu item is selected AdjustmentEvent A scroll bar is manipulated ComponentEvent A component is hidden, moved, resized, or becomes visible ContainerEvent A component is added to or removed from a container FocusEvent A component gains or loses keyboard focus Table 62.1 Event classes http://improvejava.blogspot.in/ 9
  • 10. Event Classes contd.. InputEvent Abstract super class for all component input event classes ItemEvent A check box or list item is clicked; also occurs when a choice selection is made or a checkable menu item is selected or deselected KeyEvent Input is received from the keyboard MouseEvent The mouse is dragged, moved, clicked, pressed, or released; also generated when the mouse enters or exits a component TextEvent The value of a text area or text field is Changed WindowEvent Window is activated, closed, deactivated, deiconified, iconified, opened, or quit Table 62.2 Event classes http://improvejava.blogspot.in/ 10
  • 11. Event Sources • A source is an object that generates an event • e.g. – A button – A TextField • Sources may generate more than one type of event • e.g . – Click on button – Double click on button http://improvejava.blogspot.in/ 11
  • 12. Event Sources contd.. • A source must register listeners in order for the listeners to receive notifications about a specific type of event • Each type of event has its own registration method • the general form: – public void addTypeListener(TypeListener el) – Here, Type is the name of the event and el is a reference to the event listener. – e.g., – the method that registers a keyboard event listener is called addKeyListener( ) – The method that registers a mouse motion listener is called addMouseMotionListener( ) http://improvejava.blogspot.in/ 12
  • 13. Event Sources contd.. • When an event occurs – All registered listeners are • Notified about the event • As well as, receive a copy of the event object • This is known as multicasting the event http://improvejava.blogspot.in/ 13
  • 14. Event Sources contd.. Event Source Generates Button action events when the button is pressed Checkbox item events when the check box is selected or deselected Choice item events when the choice is changed List action events when an item is double-clicked; generates item events when an item is selected or deselected Menu Item action events when a menu item is selected; Item events when a checkable menu item is selected or deselected Scrollbar adjustment events when the scroll bar is manipulated Text components text events when the user enters a character Window window events when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit. Table 62.3 Event sources http://improvejava.blogspot.in/ 14
  • 15. Event Listeners • A listener is an object that is notified when an event occurs • It has two major requirements – it must have been registered with one or more sources to receive notifications – it must implement methods to receive and process these notifications http://improvejava.blogspot.in/ 15
  • 16. Event Listeners contd.. • Listeners are created by implementing one or more of the interfaces • When an event occurs, the event source invokes the appropriate method defined by the listener and provides an event object as its argument http://improvejava.blogspot.in/ 16
  • 17. Event Listeners contd.. Interface Description ActionListener Defines one method to receive action events AdjustmentListener Defines one method to receive adjustment events ComponentListener Defines four methods to recognize when a component is hidden, moved, resized, or shown ContainerListener Defines two methods to recognize when a component is added to or removed from a Container FocusListener Defines two methods to recognize when a component gains or loses keyboard focus ItemListener Defines one method to recognize when the state of an item changes KeyListener Defines three methods to recognize when a key is pressed, released, or typed Table 62.4 Event Listeners http://improvejava.blogspot.in/ 17
  • 18. Event Listeners contd.. Interface Description MouseListener Defines five methods to recognize when the mouse is clicked, enters a component, exits a component, is pressed, or is released MouseMotionListener Defines two methods to recognize when the mouse is dragged or moved TextListener Defines one method to recognize when a text value Changes WindowListener Defines seven methods to recognize when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit Table 62.5 Event Listeners http://improvejava.blogspot.in/ 18
  • 19. Summary • In this class we discussed – Event handling – Delegation event model – Events – Event Sources – Event Listeners http://improvejava.blogspot.in/ 19
  • 20. Quiz 1. What is the event source for AdjustmentEvent ? a) TextField b) List c) ScrollBar d) Button http://improvejava.blogspot.in/ 20
  • 21. Quiz contd.. 2. EventObject is defined in which package ? a) java.awt b) java.awt.event c) java.util d) java.applet http://improvejava.blogspot.in/ 21
  • 22. Frequently Asked Questions 1. Explain the delegation model of event handling 2. List some example for event sources 3. What is event listeners? List some examples for event listener http://improvejava.blogspot.in/ 22