Event handling                 http://improvejava.blogspot.in/                                                   1
ObjectivesOn completion of this period, you would beable to know• Event handling• Delegation event model• Events• Event So...
RecapIn the previous class, you have leant• The AWT classes                http://improvejava.blogspot.in/                ...
Event Handling• The AWT based programs and applets are  event-driven programs• Event handling is at the core of success of...
The Delegation Event Model• This model defines   – standard and consistent mechanisms to generate and     process events• ...
The Delegation Event Model                      contd..• The advantages of this model are  – The application logic that pr...
Events• An event is an object that describes a state  change in a source• It can be generated as a consequence of a  perso...
Event Classes• The classes that represent events are at the  core of Java’s event handling mechanism• EventObject  – Defin...
Event Classes                          contd..• The package java.awt.event defines several types of  events that are gener...
Event Classes                          contd..InputEvent       Abstract super class for all component input event         ...
Event Sources• A source is an object that generates an event• e.g.  – A button  – A TextField• Sources may generate more t...
Event Sources                               contd..• A source must register listeners in order for the listeners  to recei...
Event Sources                              contd..• When an event occurs  – All registered listeners are     • Notified ab...
Event Sources                              contd..Event Source      GeneratesButton            action events when the butt...
Event Listeners• A listener is an object that is notified when  an event occurs• It has two major requirements  – it must ...
Event Listeners                          contd..• Listeners are created by implementing one or  more of the interfaces• Wh...
Event Listeners                           contd..      Interface                                   DescriptionActionListen...
Event Listeners                             contd..      Interface                                   DescriptionMouseListe...
Summary• In this class we discussed  – Event handling  – Delegation event model  – Events  – Event Sources  – Event Listen...
Quiz1. What is the event source for AdjustmentEvent ?  a)   TextField  b)   List  c)   ScrollBar  d)   Button             ...
Quiz                    contd..2. EventObject is defined in which package ?  a)   java.awt  b)   java.awt.event  c)   java...
Frequently Asked Questions1. Explain the delegation model of event handling2. List some example for event sources3. What i...
Upcoming SlideShare
Loading in …5
×

Event handling62

270 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
270
On SlideShare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Event handling62

  1. 1. Event handling http://improvejava.blogspot.in/ 1
  2. 2. ObjectivesOn completion of this period, you would beable to know• Event handling• Delegation event model• Events• Event Sources• Event Listeners http://improvejava.blogspot.in/ 2
  3. 3. RecapIn the previous class, you have leant• The AWT classes http://improvejava.blogspot.in/ 3
  4. 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. 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. 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. 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. 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. 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. 10. Event Classes contd..InputEvent Abstract super class for all component input event classesItemEvent A check box or list item is clicked; also occurs when a choice selection is made or a checkable menu item is selected or deselectedKeyEvent Input is received from the keyboardMouseEvent The mouse is dragged, moved, clicked, pressed, or released; also generated when the mouse enters or exits a componentTextEvent The value of a text area or text field is ChangedWindowEvent Window is activated, closed, deactivated, deiconified, iconified, opened, or quit Table 62.2 Event classes http://improvejava.blogspot.in/ 10
  11. 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. 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. 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. 14. Event Sources contd..Event Source GeneratesButton action events when the button is pressedCheckbox item events when the check box is selected or deselectedChoice item events when the choice is changedList action events when an item is double-clicked; generates item events when an item is selected or deselectedMenu Item action events when a menu item is selected; Item events when a checkable menu item is selected or deselectedScrollbar adjustment events when the scroll bar is manipulatedText components text events when the user enters a characterWindow 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. 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. 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. 17. Event Listeners contd.. Interface DescriptionActionListener Defines one method to receive action eventsAdjustmentListener Defines one method to receive adjustment eventsComponentListener Defines four methods to recognize when a component is hidden, moved, resized, or shownContainerListener Defines two methods to recognize when a component is added to or removed from a ContainerFocusListener Defines two methods to recognize when a component gains or loses keyboard focusItemListener Defines one method to recognize when the state of an item changesKeyListener Defines three methods to recognize when a key is pressed, released, or typed Table 62.4 Event Listeners http://improvejava.blogspot.in/ 17
  18. 18. Event Listeners contd.. Interface DescriptionMouseListener Defines five methods to recognize when the mouse is clicked, enters a component, exits a component, is pressed, or is releasedMouseMotionListener Defines two methods to recognize when the mouse is dragged or movedTextListener Defines one method to recognize when a text value ChangesWindowListener 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. 19. Summary• In this class we discussed – Event handling – Delegation event model – Events – Event Sources – Event Listeners http://improvejava.blogspot.in/ 19
  20. 20. Quiz1. What is the event source for AdjustmentEvent ? a) TextField b) List c) ScrollBar d) Button http://improvejava.blogspot.in/ 20
  21. 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. 22. Frequently Asked Questions1. Explain the delegation model of event handling2. List some example for event sources3. What is event listeners? List some examples for event listener http://improvejava.blogspot.in/ 22

×