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.

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.
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 Handling.
Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
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 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.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
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.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
GUI Programming in Java
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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,
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
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,
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.
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.
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.
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.
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.
Layout Managers Arranges and lays out the GUI components on a container.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
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
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.
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.
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.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
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.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
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.
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.
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.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
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
Chapter 12 Event-Driven Programming
Computer Science 209 Graphics and GUIs.
Programming in Java Event Handling
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
Chapter 12 Event Handling
Final project.
Presentation transcript:

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 for input in a specific order

Event based input The user is in control of the sequencing Mouse Keyboard pull down menus click buttons etc. IN ANY ORDER!

Java event handling The Java window toolkit has a sophisticated mechanism that allows a program to specify the event Whenever the user of a graphical program types characters or uses the mouse the Java window managers sends a notification to the program that an event has occurred

Events User interface events include key presses, mouse moves, button presses, etc. As the programmer, you supply event listeners (objects) for the events you care about. You need to implement methods for handling the events in the event listener you supply You construct listener objects.

Event Listeners Event Listeners are objects of a class You construct listener objects and attach them to the event sources – “registration” Event sources are things like text boxes, buttons, etc Sources of events keep track of the type of things they listen for

Event Notification Event notifications happen in event listener classes. An event listener class implements an event listener interface When an event occurs the event source will call the methods you supplied in the listener The invocation of the method will be supplied with information about the event in an event class object

The listener is an object Sometimes an object can be two things an applet a listener Sometimes, we make an entirely different class

MouseListener public interface MouseListener { void mousePressed(MouseEvent event); void mouseReleased(MouseEvent event); void mouseClicked(MouseEvent event); void mouseEntered(MouseEvent event); void mouseExited(MouseEvent event); }

MouseSpy MouseSpyMouseListener mousePressed MouseEvent mouseReleased MouseEvent mouseClicked MouseEvent mouseEntered mouseExited MouseEvent

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()); }

public class Lec14 extends Applet { public Lec14() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); }

Components Java supports things like buttons and fields The java awt can vary from machine to machine Swing is more platform independent

Event Registration JTextField object textField.addActionListener(handler); TextFieldHandler object public void actionPerformed ( ActionEvent event) { // deal with it } listenerListhandlertextfield1 …

Example Applet Ted Pawlicki's CSC 171 Applet HOW DOES IT WORK?

import java.awt.*; import java.applet.*; import java.awt.event.*; public class Lec14b extends Applet implements ActionListener { private Label prompt1, prompt2, prompt3, greeting; private TextField firstname, lastname; WHAT IS THIS THING?

public Lec14b() { setLayout(null); prompt1 = new Label("Welcome to Ted Pawlicki's CSC 171 Lec 14b Applet"); prompt2 = new Label("Please enter your first name"); prompt3 = new Label("Please enter your last name"); greeting = new Label(); firstname = new TextField(); lastname = new TextField();

add(prompt1); prompt1.setBounds(150,50,350,32); add(prompt2); prompt2.setBounds(150,100,150,32); add(firstname); firstname.setBounds(150,150,150,32); add(prompt3); prompt3.setBounds(150,200,150,32); add(lastname); lastname.setBounds(150,250,150,32);

firstname.addActionListener(this); lastname.addActionListener(this); }

public void actionPerformed (ActionEvent event){ greeting.setText("Nice to meet you, " + firstname.getText() + " " + lastname.getText()); add(greeting); greeting.setBounds(150,300,350,32); doLayout(); } WHAT IS GOING ON?

public void actionPerformed (ActionEvent event){ greeting.setText("Nice to meet you, " + firstname.getText() + " " + lastname.getText()); System.out.println("Event : " + event);// add(greeting); greeting.setBounds(150,300,350,32); doLayout(); } WHAT IS THE EVENT?

Inner Classes

public JButton makeButton(String label, final int dx,final int dy) { JButton button = new JButton(label); class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { box.translate(dx, dy); repaint(); } }; ButtonListener listener = new ButtonListener(); button.addActionListener(listener); return button; }

panel.add(makeButton("Left",-BOX_WIDTH,0)); panel.add(makeButton("Right",BOX_WIDTH,0)); panel.add(makeButton("Up",0,-BOX_HEIGHT)); panel.add(makeButton("Down",0,BOX_HEIGHT));