Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Introduction. Event Model. Creating GUI Application. Event Examples.

Similar presentations


Presentation on theme: "Agenda Introduction. Event Model. Creating GUI Application. Event Examples."— Presentation transcript:

1

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

3 Introduction

4 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

5 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

6 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

7 Event Object All event information are stored in object called Event object. Information examples :  Source of event.  Time when event occurred. 7 - 7

8 Event Object Classes 7 - 8

9 Event Object Examples 7 - 9

10 Event Model

11 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

12 Event Model Components Event model contains three important players:  Event Source.  Event Listener/Handler.  Event Object 7 - 12

13 Event Source Event source GUI component that generates the event Examples: Button, TextField and ComboBox. 7 - 13

14 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

15 Listener Methods 7 - 15

16 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

17 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

18 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

19 Delegation Model Flow Event Source Event Listener 1- Source Register Listener 3- Reacts to the Event 2- Fire an Event Object 7 - 19

20 Creating GUI Application

21 GUI Example To create A GUI Application with event handling 1.Create a GUI. 2.Create Listener. 3.Register listener with the source. 7 - 21

22 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

23 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

24 2- Create Listener cont. 7 - 24

25 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 Listener method of the event source. Example:  JButton b =new JButton(“ADD”);  b.addActionListener(listener); 7 - 25

26 Event Examples

27 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

28 Handling Action Event Java provides a listener interface ActionListener to handle action events. You must override actionPerformed (ActionEvent e) method. 7 - 28

29 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

30 Handling Item Event Java provides a listener interface ItemListener to handle Item events. You must override itemStateChanged (ItemEvent e) method. 7 - 30

31 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

32 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

33 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

34 Focus Event An action event occurs when the Component :  Gain focus.  Lose focus. java.awt.event.FocusEvent +getSource() : Object 7 - 34

35 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

36 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

37 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

38 Questions

39 Thanks


Download ppt "Agenda Introduction. Event Model. Creating GUI Application. Event Examples."

Similar presentations


Ads by Google