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

Post on 13-Dec-2015

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Event Handling

Agenda

• Introduction.

• Event Model.

• Creating GUI Application.

• Event Examples.

Introduction

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

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

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

Event Object

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

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

7 - 7

Event Object Classes

7 - 8

Event Object Examples

7 - 9

Event Model

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

Event Model Components

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

7 - 12

Event Source

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

7 - 13

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

Listener Methods

7 - 15

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

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

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

Delegation Model Flow

Event Source

Event Listener

1- Source Register Listener

3- Reacts to the Event

2- Fire an Event Object

7 - 19

Creating GUI Application

GUI Example

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

7 - 21

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

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

2- Create Listener cont.

7 - 24

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

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

Handling Action Event

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

• You must override actionPerformed (ActionEvent e) method.

7 - 28

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

Handling Item Event

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

• You must override itemStateChanged (ItemEvent e) method.

7 - 30

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

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

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

Focus Event

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

java.awt.event.FocusEvent

+getSource() : Object

7 - 34

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

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

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

Questions

Thanks

top related