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

Similar documents
GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

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

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

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

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

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

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

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

The AWT Event Model 9

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

Swing from A to Z Some Simple Components. Preface

MIT AITI Swing Event Model Lecture 17

Packages: Putting Classes Together

INTRODUCTION TO (GUIS)

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

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Dr. Hikmat A. M. AbdelJaber

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

ITEC 120 4/14/11. Review. Need. Objectives. GUI basics JFrame JPanel JLabel, JButton, JTextField Layout managers. Lecture 38 GUI Interactivity

GUI in Java TalentHome Solutions

PIC 20A GUI with swing

SINGLE EVENT HANDLING

Class 16: The Swing Event Model

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Original GUIs. IntroGUI 1

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

GUI Event Handlers (Part I)

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

AP CS Unit 11: Graphics and Events

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

RAIK 183H Examination 2 Solution. November 11, 2013

H212 Introduction to Software Systems Honors

Graphical Interfaces

Computer Science 210: Data Structures. Intro to Java Graphics

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Event Driven Programming

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

Advanced Java Programming (17625) Event Handling. 20 Marks

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

CSE 331 Software Design & Implementation

Graphical User Interface (GUI)

COMPSCI 230. Software Design and Construction. Swing

Graphical Applications

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

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

CS108, Stanford Handout #22. Thread 3 GUI

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

To gain experience using GUI components and listeners.

Midterm assessment - MAKEUP Fall 2010

RAIK 183H Examination 2 Solution. November 10, 2014

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

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

Event Handling in JavaFX

CS 251 Intermediate Programming GUIs: Event Listeners

Graphical User Interfaces 2

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

SD Module-1 Advanced JAVA

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...

Part I: Learn Common Graphics Components

SD Module-1 Advanced JAVA. Assignment No. 4

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

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

Come & Join Us at VUSTUDENTS.net

Lecture 19 GUI Events

Swing from A to Z Using Focus in Swing, Part 2. Preface

Graphical User Interfaces (GUIs)

Graphical User Interface (GUI)

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS


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

Graphical Interfaces

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

CS 106A, Lecture 23 Interactors and GCanvas

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

6 The MVC model. Main concepts to be covered. Pattern structure. Using design patterns. Design pattern: Observer. Observers

1005ICT Object Oriented Programming Lecture Notes

Programming Languages and Techniques (CIS120)

Graphical User Interfaces 2

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

Introduction to the JAVA UI classes Advanced HCI IAT351

Introduction. Introduction

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

Graphical User Interfaces 2

Java. GUI building with the AWT

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

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

News and info. Array. Feedback. Lab 4 is due this week. It should be easy to change the size of the game grid.

CHAPTER 2. Java Overview

Systems Programming Graphical User Interfaces

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

CSC207 Week 4. Larry Zhang

More Swing. CS180 Recitation 12/(04,05)/08

CS 251 Intermediate Programming GUIs: Components and Layout

Window Interfaces Using Swing Objects

Transcription:

CS211 GUI Dynamics Announcements Prelim 2 rooms: A-M are in Olin 155 N-A are in Olin 255 Final exam: final exam 5/17, 9-11:30am final review session (TBA, likely Sun 5/15) Consulting: regular consulting ends Thur, May 5 special consulting/office hours afterwards (TBA) 1 2 Motivation/Overview Example Revisted Reminders GUI statics: painting Components in Containers on computer screen GUI dynamics: causing and responding to actions What actions? called events need to write code that "understands" how to handle them and what do objects that handle events must "hear" the events and have methods that know what to do for each event What objects? events and listeners overview: see Intro.java from last time... 3 import.*; public class Intro extends JFrame { private JButton b = new JButton("Push Me!"); private JLabel label = new JLabel(generateLabel()); Intro f = new Intro(); f.setdefaultcloseoperation(jframe.exit_on_close); f.setsize(200,100); f.setvisible(true); public Intro () { setlayout(new FlowLayout(FlowLayout.LEFT) ); add(label); b.addactionlistener( new ActionListener() { label.settext(generatelabel()); ) ; return "Count: "+Integer.toString(count); 4 1

Delegation Model (Intro) Events Roadmap for learning GUI dynamics: user/program does something to component... Java issues an event object... A special type of object "hears" that event... That listener has a method that "handles" the event The handler does whatever the programmer programmed So... what do you need to learn? events: how to make components issue events listeners: how to make a component listen for events handlers: how to write a method that deals with events start with events... Event object (or, event): signal to program that an action has occurred Java creates an internal object (the event object) examples: mouse clicked, button pushed, menu selected API classes for event objects: event object ancestor: java.util.event most events you need are in some events are in javax.swing.event Portion of hierarchy: Event AWTEvent ComponentEvent InputEvent MouseEvent KeyEvent java.util java.awt 5 6 Event Source Source and Event s What kinds of events can be issued? user interacts with a component the component generates the event (an object) define special object: event source the object on which the user generates an event usually components (see GUI statics), but could be other objects User Action click button select menu item dialog window Event Source JButton JMenuItem JDialog Event WindowEvent How to connect? event objects can identify their types and source objects useful method inherited from Event: getsource() return the source object of the Event example) user could press multiple buttons: if (e.getsource()==button1) { /* do something */ else if (e.getsource()==button2) { /* do something else */ // and so on example) see actionperformed(...) in Intro Still need special objects to listen for the events... 7 8 2

Event Listeners Listener Interfaces Delegation model revisted: user acts on source object source object generates event object listener object acts on the generated event Event listener (or listener object, or just listener): object that can hear (receive) an event object designed to perform actions based on events (hint: see previous slide) need to register listeners with components To make listener objects, you need listener classes: Java provides listener interfaces that you implement By implementing a listener interface, a class can provide listener objects for...you guessed it!...listening Listener interfaces: typical pattern: TypeEventTypeListener eg) ActionListener Types of listeners: see java.util.eventlistener How to implement a listener...? User Source Event Listener Action trigger event create event notify listener 9 10 Implementing Listener Interface Examples Which class should be a listener? typical choices: top-level container that "contains" whole GUI public class MyGUI extends JFrame implements ActionListener inner classes to create specific listeners for reuse private class LabelMaker implements ActionListener anonymous classes for "on the spot" b.addactionlistener(new ActionListener() {...); Listeners and handlers: consequence of implementing an interface: must implement that interface's methods listener's methods are called handlers: methods that handle event objects heard by listeners Some listeners and their handlers: ActionListenermust implement void actionperformed( e) KeyListenermust implement void keypressed(keyevent e) void keyreleased(keyevent e) void keytyped(keyevent e) Identifying source object: getsource() (from java.util.event) see specific event classes for other methods 11 12 3

Registering Listeners Rules and Examples How does a component know which listener to use? You must register listeners: must connect listener objects to source objects connection process called registering listeners you write code that adds listeners to a component Syntax: component.addtypelistener(listener) examples) b.addactionlistener(this) /* GUI class is also a listener */ /* handlers use method, like event.getsource() to identify source objects */ b.addactionlistener(new ActionListener() { /* handler */ ); /* define handler "on the spot" */ Rules: source object could notify many listeners register multiple listeners to source object multiple source objects can share same listener ex) GUI class is listener use getsource to identify source object http://java.sun.com/docs/books/tutorial/uiswing/events/generalrules.html Some examples? no inner classes nested class anonymous class 13 14 Example 1: no inner classes // Counter1: frame implements listener import.*; class Counter1 extends JFrame implements ActionListener { private JButton b; private JLabel l; Counter1 c = new Counter1(); c.setvisible(true); public Counter1() { setgui(); setlayout(); registerlisteners(); Example 1 continued private void setgui() { setdefaultcloseoperation(jframe.exit_on_close); setsize(200,100); private void setlayout() { setlayout(new FlowLayout(FlowLayout.LEFT)); b = new JButton("Push Me!"); l = new JLabel(generateLabel()); add(l); private void registerlisteners() { b.addactionlistener(this); l.settext(generatelabel()); return "Count: "+count; 15 16 4

Example 2: nested classes import.*; public class Counter2 extends JFrame { private JButton b = new JButton("Push Me!"); private JLabel label = new JLabel(generateLabel()); Counter2 f = new Counter2(); f.setdefaultcloseoperation(jframe.exit_on_close); f.setsize(200,100); f.setvisible(true); Example 2 continued public Counter2() { setlayout( new FlowLayout(FlowLayout.LEFT) ); add(label); b.addactionlistener(new LabelMaker()); return "Count: "+count; private class LabelMaker implements ActionListener { label.settext(generatelabel()); 17 Example 3: anonymous classes see initial example in these notes (Intro) others? see website and Tutorial 18 5