event handling62

22
http:// improvejava.blogspot.in/ Event handling 1

Post on 22-Oct-2014

233 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Event handling62

http://improvejava.blogspot.in/

Event handling

1

Page 2: Event handling62

http://improvejava.blogspot.in/

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

2

Objectives

Page 3: Event handling62

http://improvejava.blogspot.in/

Recap

3

In the previous class, you have leant

• The AWT classes

Page 4: Event handling62

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 these programs

• The modern approach to handling events is based on the ‘delegation event model’

4

Page 5: Event handling62

http://improvejava.blogspot.in/

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

5

Page 6: Event handling62

http://improvejava.blogspot.in/

• 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

The Delegation Event Model contd..

6

Page 7: Event handling62

http://improvejava.blogspot.in/

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

7

Page 8: Event handling62

http://improvejava.blogspot.in/

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

8

Page 9: Event handling62

http://improvejava.blogspot.in/

• The package java.awt.event defines several types of events that are generated by various user interface elements

Event Classes contd..

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 classes9

Page 10: Event handling62

http://improvejava.blogspot.in/

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

10

Page 11: Event handling62

http://improvejava.blogspot.in/

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

11

Page 12: Event handling62

http://improvejava.blogspot.in/

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( )

12

Page 13: Event handling62

http://improvejava.blogspot.in/

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

13

Page 14: Event handling62

http://improvejava.blogspot.in/

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 sources14

Page 15: Event handling62

http://improvejava.blogspot.in/

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

15

Page 16: Event handling62

http://improvejava.blogspot.in/

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

16

Page 17: Event handling62

http://improvejava.blogspot.in/

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

Event Listeners contd..

Table 62.4 Event Listeners17

Page 18: Event handling62

http://improvejava.blogspot.in/

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

Event Listeners contd..

Table 62.5 Event Listeners

18

Page 19: Event handling62

http://improvejava.blogspot.in/

Summary

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

19

Page 20: Event handling62

http://improvejava.blogspot.in/

Quiz

1. What is the event source for AdjustmentEvent ?a) TextField

b) List

c) ScrollBar

d) Button

20

Page 21: Event handling62

http://improvejava.blogspot.in/

Quiz contd..

2. EventObject is defined in which package ?a) java.awt

b) java.awt.event

c) java.util

d) java.applet

21

Page 22: Event handling62

http://improvejava.blogspot.in/

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

22