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
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
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
Event Object All event information are stored in object called Event object. Information examples : Source of event. Time when event occurred
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
Event Model Components Event model contains three important players: Event Source. Event Listener/Handler. Event Object
Event Source Event source GUI component that generates the event Examples: Button, TextField and ComboBox
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
Listener Methods
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
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)
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 Note
Delegation Model Flow Event Source Event Listener 1- Source Register Listener 3- Reacts to the Event 2- Fire an Event Object
Creating GUI Application
GUI Example To create A GUI Application with event handling 1.Create a GUI. 2.Create Listener. 3.Register listener with the source
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
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
2- Create Listener cont
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);
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
Handling Action Event Java provides a listener interface ActionListener to handle action events. You must override actionPerformed (ActionEvent e) method
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
Handling Item Event Java provides a listener interface ItemListener to handle Item events. You must override itemStateChanged (ItemEvent e) method
Mouse Event An action event occurs when the user uses the mouse to interact with a component 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 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 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
Handling Focus Event Java provides a listener interface ItemListener to handle Item events. You must override focusLost (focusEvent e) and focusGained (focusEvent e) methods
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
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 java.awt.event.KeyListener +keyPressed (e : KeyEvent ) : void +keyReleased (e : KeyEvent ) : void +keyTyped (e : KeyEvent ) : void
Questions
Thanks