Events and Event Handling

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

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
Event Handling.
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
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. 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.
Event-Driven Programming
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.
Intermediate Java1 An example that uses inner classes Week Four Continued.
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
Event Handling in Java CSE Software Engineering Spring 2000 By Prasad.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
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.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
For (int i = 1; i
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Introduction to Java Programming
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
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.
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.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CS Lecture 04 Mice Lynda Thomas
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
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.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
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.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Lesson 28: More on the GUI button, frame and actions.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
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 1: JFC and Swing Basics OOP tirgul No
CSC 205 Programming II Lecture 5 AWT - I.
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Chapter 12 Event-Driven Programming
GUI III IS
Programming in Java Event Handling
Ellen Walker Hiram College
GUI Event Handling Nithya Raman.
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
Event Driven Programming
Web Design & Development Lecture 12
Chapter 16 Event-Driven Programming
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Making Java GUIs Functional
Final project.
Presentation transcript:

Events and Event Handling CS140 Dick Steflik

series of events GUI is displayed and system is running it's event loop user does something (clicks mouse button) system recognizes whatever it was and creates an appropriate event object (MouseEvent) and passes it to the handler that has been registered for that event (mouseClicked()) the handler executes the handling code system returns to event loop handlers are registered with the system when a listener is added to a component

java.awt.event provides interfaces and classes for handling events generated by AWT components

ActionEvent An event which indicates that a component-defined action occurred. This high-level event is generated by a component (such as a Button) when the component-specific action occurs (such as being pressed). The event is passed to every ActionListener object that registered to receive such events using the component's addActionListener method.

Swing Listeners action listeners – listens for user actions on registered components component listener – listens for changes in the component's size, position or visibility focus listener – listens for whether the component gained or lost the keyboard focus key listener – listens for key presses, key events are fired by the component currently having keyboard focus mouse listener – listens for mouse clicks, mouse presses, mouse releases or mouse movement into or out of the components drawing area mouse-motion listener – listens for changes in the mouse cursor's position over the component mouse-wheel listener - listens for mouse wheel movement over the component Hierarchy listener – listens for changes to a component's containment hierarchy of changed events Hierarchy bounds listener – listens for changes in a component's containment hierarchy of moved and resized events

Listener Interfaces Listeners are defined as java interfaces with a listener interface for each event type ex the ActionListener interface has only one method: actionPerformed(ActionEvent e) the MouseListener has five methods: mouseClicked(MouseEvent e) mouseEntered(MouseEvent e) mouseExited(MouseEvent e) mousePressed(MouseEvent e) mouseReleased(MouseEvent e) all method must be defined (even if with an empty body) unless tou use an Adapter class

Adapters Adapter is a pattern that provides default (often empty) implementation of interface methods or abstract classes. For example MouseAdapter provides empty implementation of MouseListener interface methods. You only need to override the methods you need to implement. It is useful because very often you do not really use all methods declared by interface, so implementing the interface directly is very wordy.

button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // do something. } });