agenda introduction. event model. creating gui application. event examples

39
Event Handling

Upload: cassandra-goodman

Post on 13-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Event Handling

Page 2: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Agenda

• Introduction.

• Event Model.

• Creating GUI Application.

• Event Examples.

Page 3: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Introduction

Page 4: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Event-Driven Programming

• In Procedural programming : Code is executed in procedural order.

• In event-driven programming : Code is executed upon activation of events.

7 - 4

Page 5: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

What’s Event

• Event is a type of signal to tell the program that something has happened.

• Event is generated by External user actions such as mouse movements, mouse

clicks, and keystrokes . Operating system, such as a timer.

7 - 5

Page 6: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Event Source

• The component on which an event is fired is called: Source object or source component.

• Examples A button is the source object for a button-clicking event. A ComboBox it the source object for selecting an item.

7 - 6

Page 7: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Event Object

• All event information are stored in object called Event object.

• Information examples : Source of event. Time when event occurred.

7 - 7

Page 8: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Event Object Classes

7 - 8

Page 9: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Event Object Examples

7 - 9

Page 10: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Event Model

Page 11: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Delegation Event Model

• Model used by Java to handle user interaction with GUI components.

• Describes how your program can respond to user interaction.

7 - 11

Page 12: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Event Model Components

• Event model contains three important players: Event Source. Event Listener/Handler. Event Object

7 - 12

Page 13: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Event Source

• Event source GUI component that generates the event• Examples: Button , TextField and ComboBox.

7 - 13

Page 14: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Event Listener/Handler

• Receives and handles events.• Contains business logic.• Examples:

Computing the summation of two numbers. Computing the maximum of two numbers. Displaying information useful to the user.

7 - 14

Page 15: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Listener Methods

7 - 15

Page 16: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Event Object

• Created when an event occurs - user interacts with a GUI component.

• Contains all necessary information about the event that has occurred. Type of event that has occurred. Source of the event. Time when event occurred.

• Represented by an Event class.

7 - 16

Page 17: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Registration

• A listener should be registered with a source.• Once registered, listener waits until an event occurs.• When an event occurs:

An event object created by the event source. Event object is fired by the event source to the registered

listeners. Method of event listener is called with an event object as a

parameter(Business logic).

7 - 17

Page 18: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Registration cont.

• Once the listener receives an event object from the source: Understand the event. Processes the event that occurred.

Event source can be registered to one or more listener.

7 - 18

Note

Page 19: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Delegation Model Flow

Event Source

Event Listener

1- Source Register Listener

3- Reacts to the Event

2- Fire an Event Object

7 - 19

Page 20: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Creating GUI Application

Page 21: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

GUI Example

• To create A GUI Application with event handling1. Create a GUI.2. Create Listener.3. Register listener with the source.

7 - 21

Page 22: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

1- Create GUI

• In this step we create the GUI and adding all needed components.

• The GUI Describes and displays the appearance of the application.

7 - 22

Page 23: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

2- Create Listener

• Listener is a class implementing the appropriate listener interface. Override all methods of the appropriate listener interface. Describe in each method how you would like the event to be

handled. May give empty implementations for methods you don't

need.

7 - 23

Page 24: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

2- Create Listener cont.

7 - 24

Page 25: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

3- Registeration

• Register the listener object with the event source. The object is an instantiation of the listener class in step 2 Use the add<Type>Listener method of the event source.

• Example: JButton b =new JButton(“ADD”); b.addActionListener(listener);

7 - 25

Page 26: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Event Examples

Page 27: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Action Event

• An action event occurs when the user : Clicks a button. Chooses a menu item. Presses Enter in a text field .

java.awt.event.ActionEvent

+getActionCommand( ) : String

+getSource( ) : Object

+getWhen( ) : long

7 - 27

Page 28: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Handling Action Event

• Java provides a listener interface ActionListener to handle action events.

• You must override actionPerformed (ActionEvent e) method.

7 - 28

Page 29: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Item Event

• An action event occurs when the user : Check a Checkbox, RadioButton, menu items. Select a ComboBox .

java.awt.event.ItemEvent

+getSource( ) : Object

+getWhen( ) : long

+getStateChange( ) : int

7 - 29

Page 30: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Handling Item Event

• Java provides a listener interface ItemListener to handle Item events.

• You must override itemStateChanged (ItemEvent e) method.

7 - 30

Page 31: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Mouse Event

• An action event occurs when the user uses the mouse to interact with a component.

7 - 31

java.awt.event.MouseEvent

+getWhen( ) : long

+getClickCount( ) : int

+getX( ) : int

+getY( ) : int

+getButton( ) : int

Page 32: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Handling Mouse Event

• Java provides two listener interfaces, MouseListener and MouseMotionListener to handle mouse events.

• The MouseMotionListener listens for actions such as dragging or moving the mouse.

7 - 32

java.awt.event.MouseMotionListener

+mouseDragged (e : MouseEvent ) : void

+mouseMoved (e : MouseEvent ) : void

Page 33: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Handling Mouse Event

• The MouseListener listens for actions such as when the mouse is pressed, released, entered, exited, or clicked.

7 - 33

java.awt.event.MouseListener

+mousePressed (e : MouseEvent ) : void

+mouseReleased (e : MouseEvent ) : void

+mouseClicked (e : MouseEvent ) : void

+mouseEntered (e : MouseEvent ) : void

+mouseExit (e : MouseEvent ) : void

Page 34: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Focus Event

• An action event occurs when the Component : Gain focus. Lose focus.

java.awt.event.FocusEvent

+getSource() : Object

7 - 34

Page 35: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Handling Focus Event

• Java provides a listener interface ItemListener to handle Item events.

• You must override focusLost (focusEvent e) and focusGained (focusEvent e) methods.

7 - 35

Page 36: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Key Event

• An action event occurs when the user is typing on keyboard.

• Keys KeyEvent.VK_Home for home. KeyEvent.VK_1 for 1. KeyEvent.VK_H for h etc.. .

java.awt.event.KeyEvent

+getKeyChar( ) : char

+getKeyCode( ) : int

7 - 36

Page 37: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Handling Key Event

• Java provides a listener interface KeyListener to handle Item events.

• The KeyListener listens for actions such as when the key is pressed, released, or typed.

7 - 37

java.awt.event.KeyListener

+keyPressed (e : KeyEvent ) : void

+keyReleased (e : KeyEvent ) : void

+keyTyped (e : KeyEvent ) : void

Page 38: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Questions

Page 39: Agenda Introduction. Event Model. Creating GUI Application. Event Examples

Thanks