UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
TCU CoSc Programming with Java Handling Events.
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.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
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.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
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.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
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.
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.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
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.
For (int i = 1; i
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.
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.
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.
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.
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,
Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
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.
Event Handling and Listeners in SWING The practice of event handling.
1 Lecture 8: User Interface Components with Swing.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
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.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 14 Event-Driven Programming
Welcome To java
CompSci 230 S Programming Techniques
CHAPTER Reacting to the user.
Chapter 12 Event-Driven Programming
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Lecture 09 Applets.
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 16 Event-Driven Programming
Events, Event Handlers, and Threads
Making Java GUIs Functional
Chapter 6 Inheritance.
Presentation transcript:

UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out the required task. This is Event Handling Execution

UQC117S2 Graphics Programming Lecture 2 How Can This Happen? Bottom level: Software cycles round in a loop, waiting for an event. Event loop JAVA does this for us. All we have to do is implement the methods that JAVA invokes when an event occurs –In invoking these methods, JAVA also provides useful info. about the event

UQC117S2 Graphics Programming Lecture 2 How Do Events Get Captured? 3 things to do –Declare that the class implements the relevant listener interface –Implement the necessary listener methods –Register that a component is going to listen out for an event

UQC117S2 Graphics Programming Lecture 2 Interfaces –“Defines a protocol of behaviour” –Allows JAVA to provide a sort of multiple inheritance, without the attendant problems – The behaviour enforced by the interface is that these methods must be implemented.

UQC117S2 Graphics Programming Lecture 2 Example The MouseListener interface defines the following 5 abstract methods: –mouseClicked(MouseEvent) –mouseEntered(MouseEvent) –mouseExited(MouseEvent) –mousePressed(MouseEvent) –mouseReleased(MouseEvent) A class that declares that it will implement the MouseListener interface is “signing a contract” to say that it will implement all 5 methods.

UQC117S2 Graphics Programming Lecture 2 Syntax public class MyClass implements MouseListener{ A class can implement several listeners. Listeners are separated by a comma. Example: public class MyClass implements WindowListener, MouseListener{ Which methods belong to each Listener? How will you know?

UQC117S2 Graphics Programming Lecture 2 Register that a class is Listening for an Event Like registering on a mailing list for an interest group By registering as an event listener, we are requiring JAVA to use the event handling methods – the signatures of which are defined in the event interface- implemented in this class to handle given events Many of the addXXXXListener methods are defined in Component, others are defined further down e.g. addWindowListener is a method belonging to the Window class Registering a class as a listener amounts to including the line: AComponent.addxxxListener(this); In your class

UQC117S2 Graphics Programming Lecture 2 Example To register that a class listens out for action events …………… Jbutton button1; …………… button1=new Jbutton(“First button”); add(button1); button1.addActionListener(this ); ………………… Why button1.addActionListener? What else must be in this class definition?

UQC117S2 Graphics Programming Lecture 2 Example Responding to an action event,generated by pressing a Jbutton Only one method to be implemented – actionPerformed The ActionEvent object in the argument has a method called getActionCommand() – returns a String public void actionPerformed(ActionEvent event) { String command; String button1 = “Button 1”; String button2 = “Button 2”; command =event.getActionCommand(); if(command.equals(button1){ System.out.println(“Button 1”); }else if (command.equals(button2){ System.out.prinltn(“Button 2”); } Check the API documentation to see what methods aresupplied with MouseEvent, ActionEvent, FocusEvent, AdjustmentEvent, ComponentEvent, ItemEvent, KeyEvent, TextEvent, and WindowEvent classes

UQC117S2 Graphics Programming Lecture 2 Implement the Necessary Listener Methods This means all of them Often seems rather untidy We can avoid this untidiness – see later Note the argument to all AWT listener methods –What is this argument? –Instance of a class descended from java.awt.AWTEvent –These classes provide methods to return useful details about the event just captured –JAVA sets up the attributes of this object, just use the method to get the details

UQC117S2 Graphics Programming Lecture 2 Adapter Classes (1) A solution to the “untidy” interface implementation For listener interfaces with > 1 method, AWT supplies an adapter class Adapters implement all of an interface’s methods as “do nothing” methods

UQC117S2 Graphics Programming Lecture 2 Adapter Classes (2) A subclass of the adapter class need therefore only implement the methods of interest Example public class MyClass extends MouseAdapter{ public void mouseClicked(MouseEvent event){ ……… (Handling of mouse click goes in here) …………… }

UQC117S2 Graphics Programming Lecture 2 Multiple Inheritance PROBLEM! If MyClass inherits from, say, JFrame, it can’t also inherit from MouseAdapter. SOLUTION! Inner classes –An inner class is a “class within a class –An inner class can be named or anonymous –If an inner class is used, the implement phrase is no longer required

UQC117S2 Graphics Programming Lecture 2 Named Inner Class public class MyClass extends Jframe{ …………… aComponent.addMouseListener(new MyAdapter)); …………… class MyAdapter extends MouseAdapter{ public void mouseClicked(MouseEvent event) {……… Handling of mouse click goes in here ……… }

UQC117S2 Graphics Programming Lecture 2 Example – Anonymous Inner Class NOT RECOMMENDED public class MyClass extends Jframe{ …………………………………… aComponent.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent event){ …………… Handling of mouse click in here ………… } }); …………… }