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

PPT
Java gui event
PDF
Unit-3 event handling
PDF
Ajp notes-chapter-03
PPTX
Event handling in Java(part 2)
PPT
09events
PPTX
Event handling
PDF
Lecture 18
PPTX
Android development session 2 - intent and activity
Java gui event
Unit-3 event handling
Ajp notes-chapter-03
Event handling in Java(part 2)
09events
Event handling
Lecture 18
Android development session 2 - intent and activity

Similar to Event handling62 (20)

PDF
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
PPT
unit-6.pptbjjdjdkd ndmdkjdjdjjdkfjjfjfjfj
PPTX
event-handling.pptx
PPTX
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
PPTX
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
PDF
Event handling
PDF
Event handling
PPT
Unit 6 Java
PDF
Event Handling in Java as per university
PPTX
Advance java programming- Event handling
PPTX
tL20 event handling
PDF
javabeans
PPTX
Module3.11.pptx
PPTX
Module 5.pptx
PPT
Chapter 8 event Handling.ppt m m m m m m
PPTX
Event handling in Java(part 1)
PDF
Java Programming :Event Handling(Types of Events)
PPTX
Event Handling in Java
PPTX
Advance Java Programming(CM5I) Event handling
PDF
java-programming GUI- Event Handling.pdf
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
unit-6.pptbjjdjdkd ndmdkjdjdjjdkfjjfjfjfj
event-handling.pptx
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
Event handling
Event handling
Unit 6 Java
Event Handling in Java as per university
Advance java programming- Event handling
tL20 event handling
javabeans
Module3.11.pptx
Module 5.pptx
Chapter 8 event Handling.ppt m m m m m m
Event handling in Java(part 1)
Java Programming :Event Handling(Types of Events)
Event Handling in Java
Advance Java Programming(CM5I) Event handling
java-programming GUI- Event Handling.pdf
Ad

More from myrajendra (20)

PPT
Fundamentals
PPT
Data type
PPTX
Hibernate example1
PPTX
Jdbc workflow
PPTX
2 jdbc drivers
PPTX
3 jdbc api
PPTX
4 jdbc step1
PPTX
Dao example
PPTX
Sessionex1
PPTX
Internal
PPTX
3. elements
PPTX
2. attributes
PPTX
1 introduction to html
PPTX
Headings
PPTX
Forms
PPT
PPTX
Views
PPTX
Views
PPTX
Views
PPT
Starting jdbc
Fundamentals
Data type
Hibernate example1
Jdbc workflow
2 jdbc drivers
3 jdbc api
4 jdbc step1
Dao example
Sessionex1
Internal
3. elements
2. attributes
1 introduction to html
Headings
Forms
Views
Views
Views
Starting jdbc
Ad

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