IPM 12/13 P4 Handling Events in AWT

Similar documents
Interacción con GUIs

The AWT Event Model 9

GUI in Java TalentHome Solutions

COMPSCI 230. Software Design and Construction. Swing

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Computer Programming Java AWT Lab 18

Event Driven Programming

10/16/2008. CSG 170 Round 5. Prof. Timothy Bickmore. User Analysis Task Analysis (6) Problem Scenarios (3)

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

EVENTS, EVENT SOURCES AND LISTENERS

Assignment No 2. Year: Dept.: ComputerTech. Sanjivani Rural Education Society s Sanjivani KBP Polytechnic, Kopargaon

Programming Mobile Devices J2SE GUI

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

GUI Programming: Swing and Event Handling

Cheng, CSE870. More Frameworks. Overview. Recap on OOP. Acknowledgements:

Mining framework concepts from application code

Java Applets / Flash

Advanced Java Programming (17625) Event Handling. 20 Marks

CHAPTER 2. Java Overview

III/IV B.Tech (Regular/Supplementary) DEGREE EXAMINATION. Electronics & Instrumentation Engineering Object Oriented Programming with JAVA

Java. GUI building with the AWT

Dr. Hikmat A. M. AbdelJaber

GUI Event Handlers (Part I)

AWT TEXTFIELD CLASS. Constructs a new empty text field with the specified number of columns.

Java Event Handling -- 1

SD Module-1 Advanced JAVA

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

SD Module-1 Advanced JAVA. Assignment No. 4

CS211 GUI Dynamics. Announcements. Motivation/Overview. Example Revisted

Mobile MOUSe JAVA2 FOR PROGRAMMERS ONLINE COURSE OUTLINE

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Abstract Classes and Interfaces

G51PRG: Introduction to Programming Second semester Applets and graphics

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

กล ม API ท ใช. Programming Graphical User Interface (GUI) Containers and Components 22/05/60

Overloading Example. Overloading. When to Overload. Overloading Example (cont'd) (class Point continued.)

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

Basicsof. JavaGUI and SWING

JAVA. Object Oriented Programming with. M.T. Somashekara D.S.Guru K.S. Manjunatha

Java AWT Windows, Text, & Graphics

BM214E Object Oriented Programming Lecture 13

CORBA Java. Java. Java. . Java CORBA. Java CORBA (RMI) CORBA ORB. . CORBA. CORBA Java

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

The AWT Component Library 10

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Graphical User Interface

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

Sri Vidya College of Engineering & Technology

Programming Languages and Techniques (CIS120)

CS11 Java. Fall Lecture 3

Programming Languages and Techniques (CIS120e)

Example: CharCheck. That s It??! What do you imagine happens after main() finishes?

MIT AITI Swing Event Model Lecture 17

MODULE 8p - Applets - Trials B

CS 106A, Lecture 23 Interactors and GCanvas

Introduction. Introduction

READ AND OBSERVE THE FOLLOWING RULES:

JAVA PROGRAMMING COURSE NO. IT-705(A) SECTION - A

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

The Java Programming Language Basics. Identifiers, Keywords, and Types. Expressions and Flow Control. Object-Oriented Programming. Objects and Classes

CSE 331 Software Design & Implementation

Java - Applets. public class Buttons extends Applet implements ActionListener

CS11 Java. Fall Lecture 4

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

14.2 Java s New Nimbus Look-and-Feel 551 Sample GUI: The SwingSet3 Demo Application As an example of a GUI, consider Fig. 14.1, which shows the SwingS

1 GUI GUI GUI GUI GUI. GUI(Graphical User Interface) JDK java.awt. ? Component

Packages: Putting Classes Together

Which of the following syntax used to attach an input stream to console?

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Migrating from IBM VisualAge to Borland 6 JBuilder

SAMPLE EXAM Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Lecture 19 GUI Events

CST141 JavaFX Events and Animation Page 1

CS Programming Language - JAVA. Fall 2004 Oct.13rd

OBJECT ORIENTED PROGRAMMING. Java GUI part 1 Loredana STANCIU Room B616

Course: CMPT 101/104 E.100 Thursday, November 23, 2000

Solutions to Chapter Exercises. GUI Objects and Event-Driven Programming

GUI Event Handlers (Part II)

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Input (part 2: input models)

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Java: Graphical User Interfaces (GUI)

Interfaces: Listeners and Adapters Java Programming 3 Lesson 6

CS 106A, Lecture 24 Interactors and NameSurfer

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

Java Applets This is not a Java course! (You re supposed to know about Java.)

Graphical User Interface (GUI)

Graphical User Interfaces in Java - SWING

Graphical Interfaces

Transcription:

IPM 12/13 P4 Handling Events in AWT Licenciatura em Ciência de Computadores Miguel Tavares Coimbra

Why Events? Components need a way to: Receive messages. Send messages. Examples: A TextField needs to know that a key was pressed. A destroy Button wants to tell the corresponding window that it was pressed. In Java AWT we can use Events

What are AWT Events? Like everything in java, an Event is an Object. java.lang.object java.util.eventobject java.awt.awtevent Based on a Consumer / Producer framework. Producer generates Event objects. Consumers register as listeners of Events.

Example Who are these? Listener1 Produces the Event Button Event Listener2 Sends it to Listeners Listener3 Who is the first to process the event? Do all listeners process it?...

Adding Listeners Producers add their Listeners And not the other way around! Function: AddListener(listener); Example: Button mybutton = new Button("Ok"); mybutton.addactionlistener(this); But this needs to know how to listen!

More examples Button button = new Button("Ok"); ActionListener ac = new ActionListener() button.addactionlistener(ac); Button button = new Button("Ok") button.addactionlistener(new ActionListner() {... );

Can I be a Listener? Yes... But you need to implement the listener interface. What is an interface? This way the Event object can be received as a parameter. public class Contador extends Frame implements ActionListener { public void actionperformed(actionevent e) { // Insert your killer code here

A more complex example Imagine you have a TextField object. You want to block every keypress that does not correspond to a number. Class mytextfield extends TextField (... ) Pause: What does extend mean?

Inheritance Object-Oriented Programming Inheritance Child classes inherit methods and properties of parent classes. class child extends parent { // killer code here Great way to reuse and expand code. Also means we must always use good programming practices since we might later reuse this code.

Back to our example Imagine you have a TextField object. You want to block every keypress that does not correspond to a number. Class mytextfield extends TextField {... So mytextfield inherits all methods and properties of TextField.

Managing Events Now mytextfield wants to listen to KeyEvents. Need to implement the KeyListener interface: public class mytextfield extends TextField implements KeyListener { public void keytyped(keyevent e) { // Do Something public void keypressed(keyevent e) { // Do Something public void keyreleased(keyevent e) { // Do Something

Consuming Events I can then implement code that ignores nonnumerical key presses. Which key was pressed? Check the KeyEvent object! Who pressed it? e.getsource(); But my Event will then be passed to TextField class. Consume the event e.consume();

Types of Events ActionEvent Button, MenuItem, TextField ItemEvent Checkbox, CheckboxMenuItem, Choice AdjustmentEvent Scrollbar WindowEvent Dialog, Frame KeyEvent Any component FocusEvent Any component ContainerEvent Container TextEvent - Text

ActionEvent example import java.applet.applet; import java.awt.button; public class TestButton extends Applet { private Button button = null; public TestButton() { super(); public void init() { button = new Button(); button.setlabel( Press me!"); button.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent e) { button.setlabel("ok"); ); add(button);

TextEvent example import java.applet.applet; import java.awt.label; import java.awt.textfield; public class TestTextField extends Applet { private TextField textfield = null; private Label label = null; public void init() { label = new Label("Password:"); textfield = new TextField(); textfield.setcolumns(10); textfield.setechochar('*'); textfield.addtextlistener(new java.awt.event.textlistener() { public void textvaluechanged(java.awt.event.textevent e) { label.settext(textfield.gettext()); ); textfield.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent e) { label.settext("password"); ); this.add(label, null); this.add(textfield, null);

Summing up I can reuse and improve previous classes NewClass extends OldClass For a class to implement an interface, it must implement all the defined methods. NewClass implements AmazingInterface Events are: Produced by components. Consumed by registered Listeners.

Resources 1. Developer Resources for Java Technology http://java.sun.com/ 2. Essentials of the Java programming language http://java.sun.com/developer/onlinetraini ng/programming/basicjava1/