Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

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 --
Event Handling.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
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.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 16 Java Fundamentals Java2 Graphical User Interfaces.
Event-Driven Programming
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an 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.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
Io package as Java’s basic I/O system continue’d.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
AWT Components (Chapter 9) Java Certification Study Group January 21, 1999 Mark Roth.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
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.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
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.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 15 Java Fundamentals Java2 Graphical User Interfaces.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
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.
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.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
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.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
AWT Layout Managers (Chapter 10) Java Certification Study Group January 21, 1999 Mark Roth.
2/17/2016 DEPT OF CSE 1 EVENT HANDLING. 2/17/2016 DEPT OF CSE 2 Event handling is at the core of successful applet programming There are several types.
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.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Event Handling CS 21a: Introduction to Computing I First Semester,
1 Lecture 8: User Interface Components with Swing.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
GUI Programming using Java - Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
Chapter 14 Event-Driven Programming
Welcome To java
CompSci 230 S Programming Techniques
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Chapter 12 Event-Driven Programming
Aum Amriteshwaryai Namah
Event Handling Chapter 2 Objectives
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Responding to Events Event Handling in Java
Lecture 09 Applets.
Chapter 15 Event-Driven Programming and Animations
Web Design & Development Lecture 12
Web Design & Development Lecture 13
Event Driven Systems and Modeling
Events, Event Handlers, and Threads
Chapter 5 Processing Input with Applets
Presentation transcript:

Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth

Events Old Model (JDK 1.0.x) –Event Model Failed Miserably New Model –Event classes –Event Listeners –Explicit event enabling –Adapters

java.util.EventObject Has no javadoc comments Defines getSource()

java.awt.AWTEvent Superclass of all AWT event classes. Event ID –int getID() –RESERVED_ID_MAX Two Ways to Handle: –Delegate Event Handling to a Listener Object –Explicitly Enable the originating Component to Handle its own Events

Subclasses of AWTEvent ActionEvent - Component Activated AdjustmentEvent - Scrollbars ContainerEvent - Components Added or Removed FocusEvent - Component Receives Input Focus ItemEvent - Item Selected from a List, Choice, Checkbox KeyEvent - Keys Pressed or Released MouseEvent - Mouse moved or buttons clicked PaintEvent - Component is Painted TextEvent - Text Component is Modified WindowEvent - Iconify, Close, Maximize, etc.

Event Listeners “...an object to which a component has delegated the task of handling a particular kind of event Create a listener class that implements the xxxListener interface 2. Construct the component 3. Construct an instance of the listener class 4. Call addxxxListener() on the component, passing in the listener object.”

Sample Code (p.313) class MyActionListener implments ActionListener { public void actionPerformed( ActionEvent ae ) { System.out.println( “Hi!” ); }

Sample Code Continued public class ListenerTest extends Applet public void init() { Button btn = new Button( “OK” ); MyActionListener listener = new MyActionListener(); btn.addActionListener( listener ); add( btn ); } }

Using Inner Classes public void init() { Button btn = new Button( “OK” ); btn.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { System.out.println( “Hi!” ); } ); }

Event Listeners Continued Eleven Listener Types (see Table 11.1) Multiple Listeners –You Can Attach as Many as You Want –No Guarantee as to Calling Order –No Guarantee as to Same Thread Removing –btn.removeActionListener( al );

Explicit Event Enabling To Hande: “ 1. Create a subclass of the component 2. In the subclass constructor, call enableEvents( AWTEvent.XXX_EVENT_MASK ) (See table 11.2 for possible event masks) 3. Provide the subclass with a processXXXEvent() method; this method should call the superclass’ version before returning.”

Sample Code (p.315) class MyBtn extends Button { public MyBtn( String label ) { super( label ); enableEvents( AWTEvent.ACTION_EVENT_MASK ); } public void processActionEvent( ActionEvent ae ) { System.out.println( “Hi!” ); super.processActionEvent( ae ); }

Listeners vs. Enabling A subclass can handle its own events by adding itself as a listener. In Explicit Event Handling, you control the order.

Adapters Event listeners often define many methods. Since they are interface, you must implement all of them. Alternative: Subclass an appropriate adapter (see table 11.3) and override only the one you need.

Sample Code (p.318) class MyIkeListener extends WindowAdapter { public void windowIconified( WindowEvent we ) { // Process the event. } // No need to declare: // windowActivated // windowClosed // windowClsing // windowDeactivated // windowDeiconified // windowOpened }

References All Material in this Presentation is heavily based on: Roberts, Simon and Heller, Philip, Java 1.1 Certification Study Guide, 1997: SYBEX™. ISBN: Selected portions from JDK JavaDoc HTML pages.