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.

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 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Introduction to Java 2 Programming
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Event Handling --
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.
Graphic User Interfaces Layout Managers Event Handling.
TCU CoSc Programming with Java Handling Events.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 12 Event-Driven Programming 找出画中真谛 — 保罗. 塞尚.
Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.
JFrame JComponent JFrame JComponent JFrame JComponent.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
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
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
1 Event-Driven Programming Just like designing GUIs, you also will probably not write Java code like the program examples given in these notes. You will.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
GUI Programming in Java: Event Handling & More Components Corresponds with Chapter 14, Chapter 15.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
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.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
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.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Introduction to Java Programming
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
Creating User Interfaces Event-Driven Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
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.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0.
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,
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
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
Chapter 12 Event-Driven Programming
Programming in Java Event Handling
GUI Programming III: Events
Event-driven programming for GUI
Web Design & Development Lecture 12
Chapter 16 Event-Driven Programming
Tonga Institute of Higher Education
Chapter 15 Event-Driven Programming and Animations Part 1
Presentation transcript:

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 movements, button clicks, and keystrokes, or by the operating system, such as a timer.  The program can choose to respond to or ignore an event. Source Object:  The component on which an event is generated is called the source object. button, button click

User Action, Source Object and Event type User ActionSource ObjectEvent Click a buttonJButtonActionEvent Press returnJTextFieldActionEvent Select itemsJListListSelectionEvent Click check boxJCheckBoxItemEvent Window openedWindowWindowEvent

A simple example Write a program that display two buttons. OK and Cancel, in a window. A message is displayed on the console to indicate which button and when it is clicked

Listeners, Registrations, and Handling Events An external user action on a source object triggers an event, and an listener receives the event. The listener must implement the corresponding event-listener interface The listener must be registered by the source object

Listener interface Java provides a listener interface for every type of GUI event. The listener interface is usually names XListener for XEvent, with excpetion of MouseMotionListener For example, listener interface for ActionEvent is ActionListener Each listener for ActionEvent should implement the ActionListener interface The listener interface contains the methods, know as the handlers, which process the events

Events, Event Listeners, and Listener Methods Event ClassListener interfaceMethods(handlers) ActionEventActionListenerActionPerformed ItemEventItemListeneritemStateChanged WindowEventWindowListenerwindowClosing KeyEventKeyListenerkyPressed MouseEventMouseListener MouseMotionListe ner mousePressed mouseDragged

Registration The listener object must be registered by the source object. Registration methods are dependent on the event type. For the ActionEvent, the method is addActionListener In general, the method is named addXListener for the XEvent ListenerClass lis = new ListenerClass(); JButton jbt – new JButton(“OK”); Jbt.addActionListener(lis);

Handling the Event When you click the button, the JButton object generates an ActionEvent and passes it to invoke the actionPerformed method to handle the event The event object contains information pertinent to the event type