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

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
TCU CoSc Programming with Java Handling Events.
Event-Driven Programming You might have realized in creating a GUI and clicking on the JButtons that nothing happens Clicking on a JButton causes the JVM.
Event Handling.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
CS221 © 2007 Ray S. Babcock Menus, Toolbars, and a Mouse Appendix C.6-C.7.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Event Handling Events and Listeners Timers and Animation.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Event-Driven Programming Event:  A signal to the program that something has happened.  It can be triggered either by external user actions, such as mouse.
Event-Driven Programming
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
1 Gui Programming (Part I) Graphical User Interfaces (Part I) l Introduction to events. l A Brief history. l Event sources and listeners. l The delegation.
Graphical User Interface (GUI) Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
For (int i = 1; i
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
UID – Event Handling and Listeners Boriana Koleva
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Object Oriented Programming.  Interface  Event Handling.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
(c) by Elizabeth Sugar Boese.1 Chapter 6 Events - Lecture Slides.
Creating User Interfaces Event-Driven Programming.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Event Handling CS 21a: Introduction to Computing I First Semester,
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming using Java - Event Handling
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Chapter 12 Event-Driven Programming
Mouse Event Handling in Java (Review)
Advanced User Interfaces
Programming in Java Event Handling
GUI Programming III: Events
Web Design & Development Lecture 12
Chapter 16 Event-Driven Programming
Making Java GUIs Functional
Presentation transcript:

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