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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Event Handling.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Listener Interfaces  A mechanism for registering handlers that respond to external events  Motivation: Develop a Canvas class that:  supports drawing.
Mouse Events and Keyboard Events
Event Handling Events and Listeners Timers and Animation.
Event-Driven Programming
OOP Java1 Event Handling Overview Listeners, Adapters and Event Sources Inner classes Event Handling Details Applets and GUI Applications 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.
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.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Chapter 12 Event Handling. Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons,
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
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.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
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.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
Fall 2006Adapded from Java Concepts Companion Slides1 Event Handling Advanced Programming ICOM 4015 Lecture 13 Reading: Java Concepts Chapter 12.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
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.
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.
Introduction to Java Programming
Understand the difference between applets and applications Identify meaningful applet resources for academic subjects Create and demonstrate a basic Java.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
CHAPTER 10 EVENT HANDLING. CHAPTER GOALS To understand the Java event model To install mouse and action listeners To accept mouse and text input To display.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01.
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,
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
Laboratory Study November, Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
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.
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.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Event Handling CS 21a: Introduction to Computing I First Semester,
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.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming using Java - Event Handling
Events and Event Handling
CompSci 230 S Programming Techniques
Java Applets.
Chapter 12 Event-Driven Programming
INFSY 547: WEB-Based Technologies
Computer Science 209 Graphics and GUIs.
GUI III IS
Programming in Java Event Handling
GUI Event Handling Nithya Raman.
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
Chapter 12 Event Handling
Java Applets.
Events, Event Handlers, and Threads
Final project.
Presentation transcript:

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 button, hitting ‘Enter’ in a text field, clicking a button …. you get the idea When an event occurs, an event object is created which stores information about that event. This event object may be utilized in the case that the program wishes to respond to the event.

The Event Class The class from which the event object is created depends upon the type of event which has occurred. For example: An object is created using the MouseEvent class if the event involved the mouse. An object is created using the ActionEvent class if a button is pressed, or textfield data has been entered. *these event classes are provided by java.awt.event package.

The Event Source The event source is the visual component with which the user has interacted. The event source creates the event object when an event has occurred. For example: an applet is the event source of a mouse click a button is the event source of a button click

The Event Listener An event listener is an object that is prepared to ‘handle’ the event, if it is notified that the event has occurred. The event listener object receives an event object when it is notified of the event, and responds to the event using the information provided in the event object.

How does it work? Each Java event source keeps an internal list of listeners. If an event occurs, the event source notifies all the listener objects on the list. Any listener that is ‘listening’ for this particular event may then respond. What does the programmer do to make a GUI respond to events? The programmer must: * provide a class from which a listener object can be created (a listener class) * create and register a listener object with an event source

Creating a ‘Listener’ class A class which is to be used for creating a mouse listener object must implement the MouseListener interface. *also in java.awt.event package public interface MouseListener{ void mousePressed(MouseEvent event); //Called when a mouse button has been pressed void mouseReleased(MouseEvent event); //Called when a mouse button has been released void mouseClicked(MouseEvent event); //Called when the mouse has been clicked void mouseEntered(MouseEvent event); //Called when the mouse enters a component void mouseExited(MouseEvent event); //Called when the mouse exits a component }

MouseSpy.java import java.awt.event.MouseEvent; import java.awt.event.MouseListener; // This listener simply prints out the listener method name // and mouse position for each mouse event public class MouseSpy implements MouseListener { public void mousePressed(MouseEvent event) { System.out.println("Mouse pressed. x = " + event.getX() + " y = " + event.getY()); } public void mouseReleased(MouseEvent event) { System.out.println("Mouse released. x = " + event.getX() + " y = " + event.getY()); }

public void mouseClicked(MouseEvent event) { System.out.println("Mouse clicked. x = " + event.getX() + " y = " + event.getY()); } public void mouseEntered(MouseEvent event) { System.out.println("Mouse entered. x = " + event.getX() + " y = " + event.getY()); } public void mouseExited(MouseEvent event) { System.out.println("Mouse exited. x = " + event.getX() + " y = " + event.getY()); }

Creating a Listener Object and ‘Registering’ It * a listener object is created just like any other – by calling a class constructor. * all event sources are required to provide methods for ‘registering’ event listener objects. We must just call that method.

MouseSpyApplet.java import java.applet.Applet; // This applet installs a mouse spy, which responds to the // five mouse events. public class MouseSpyApplet extends Applet { public MouseSpyApplet() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); //adds object listener to list of registered // event listeners for this event source }

A mouse listener class does not need to be in it’s own file … it can be a private class. public class MouseSpyApplet extends Applet { public MouseSpyApplet() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); } private class MouseSpy implements MouseListener { public void mousePressed(MouseEvent event) { //code } public void mouseReleased(MouseEvent event) { //code } public void mouseClicked(MouseEvent event) { } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } }

A mouse listener class does not need to be in it’s own file … it can be an inner class. public class MouseSpyApplet extends Applet { public MouseSpyApplet() { class MouseSpy implements MouseListener { public void mousePressed(MouseEvent event) { //code } public void mouseReleased(MouseEvent event) { //code } public void mouseClicked(MouseEvent event) { } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } } MouseSpy listener = new MouseSpy(); addMouseListener(listener); }

The constructor can still perform other tasks as well. public class AnApplet extends Applet { public AnApplet() { // initialize instance variables ********the code********** // get input from user *************the code ********** class MouseSpy implements MouseListener { // listener methods } MouseSpy listener = new MouseSpy(); addMouseListener(listener); } public void paint (Graphics g) { ******* the code************** }

Code an applet which draws a rectangle. When the mouse is clicked the rectangle should be redrawn at the mouse position. (MoveRec.java on website) Code an applet which displays two rectangles, one blue and one red, and also draws a blue ellipse. When the mouse clicks on either of the two rectangles, the rectangle color should change to the same color as the box. (ColorEllipse.java on website) The MouseAdapter class can make coding the listener classes less tedious by eliminating the need for empty methods. Just extend MouseAdapter rather than implementing MouseListener. Since a MouseAdapter implements MouseListener, any class that extends it also implements MouseListener !!

Applets can also ‘listen’ for any mouse motion. The listener object that does this implements the MouseMotionListener interface. Check it out, and code an applet which draws a filled square, such that when the mouse is placed on the square and the button clicked, the mouse drags the square with it. (MouseDrag.java on website)