Chris9an Kästner Charlie Garrod

Similar documents
Jonathan Aldrich Charlie Garrod

Charlie Garrod Michael Hilton

Introduction to concurrency and GUIs

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

Charlie Garrod Bogdan Vasilescu

Design Case Study: GUI with Swing (2)

Design Case Study: GUI with Swing

Java: Graphical User Interfaces (GUI)

Graphical User Interface (GUI)

Charlie Garrod Bogdan Vasilescu

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

Charlie Garrod Michael Hilton

CS11 Java. Fall Lecture 4

Graphical User Interface (GUI)

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

Systems Programming Graphical User Interfaces

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

Graphical interfaces & event-driven programming

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

Introduction to the JAVA UI classes Advanced HCI IAT351

Packages: Putting Classes Together

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

Graphical User Interfaces (GUIs)

Principles of So3ware Construc9on. A formal design process, part 2

Window Interfaces Using Swing Objects

Programming Languages and Techniques (CIS120)

CSE 331 Software Design & Implementation

Graphical User Interfaces

A formal design process, part 2

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

Lecture 19 GUI Events

Window Interfaces Using Swing Objects

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

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

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

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

CS 251 Intermediate Programming GUIs: Components and Layout

Building Graphical User Interfaces. GUI Principles

Graphical User Interfaces. Swing. Jose Jesus García Rueda

CS11 Java. Fall Lecture 3

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

EPITA Première Année Cycle Ingénieur. Atelier Java - J5

Part I: Learn Common Graphics Components

Java Programming Lecture 6

Graphic User Interfaces. - GUI concepts - Swing - AWT

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

SELF-STUDY. Glossary

Design for large-scale reuse: Libraries and frameworks

GUI Programming. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies

CS 251 Intermediate Programming GUIs: Event Listeners

Starting Out with Java: From Control Structures Through Objects Sixth Edition

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

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

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Architectural Requirements Phase. See Sommerville Chapters 11, 12, 13, 14, 18.2

PIC 20A GUI with swing

Graphical User Interfaces. Comp 152

Java IDE Programming-I

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

GUI Software Architecture

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Building Graphical User Interfaces. Overview

GUI Event Handlers (Part I)

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

Programming Languages and Techniques (CIS120e)

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Class 16: The Swing Event Model

CS 2113 Software Engineering

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

1005ICT Object Oriented Programming Lecture Notes

Design for Large-Scale Reuse: Libraries and Frameworks

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

2110: GUIS: Graphical User Interfaces

Lecture 9. Lecture

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Lecture 5: Java Graphics

Swing. By Iqtidar Ali

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

Window Interfaces Using Swing. Chapter 12

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

UI Software Organization

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

Instruc)onal Staff. Instructors Ron Cytron & Roger Chamberlain. Head TA Andrew Buckley. TAs there are many! See the web page

TYPES OF INTERACTORS Prasun Dewan Department of Computer Science University of North Carolina at Chapel Hill

COMPSCI 230. Software Design and Construction. Swing

Programming graphics

Charlie Garrod Bogdan Vasilescu

Josh Bloch Charlie Garrod Darya Melicher

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

DM503 Programming B. Peter Schneider-Kamp.

To gain experience using GUI components and listeners.

Transcription:

Principles of So3ware Construc9on: Objects, Design, and Concurrency (Part 3: Design Case Studies) Introduc3on to GUIs Chris9an Kästner Charlie Garrod School of Computer Science 1

Administrivia Homework 4a due tonight Homework 4b due Thursday, March 5 th Homework 3 feedback coming tonight 2

Key concepts from Tuesday 3

Key concepts from Tuesday Formal verifica9on Tes9ng Coverage metrics Black- box vs. white- box tes9ng Sta9c analysis 4

Key concepts from recita9on yesterday 5

Key concepts from yesterday's recita9on Discovering design paverns The Observer pavern 6

The Observer design pavern 7

The Observer design pavern Applicability When an abstrac9on has two interdependent aspects and you want to reuse both When state change to one object requires no9fying others, without becoming dependent on them Consequences Loose coupling between subject and observer, enhancing reuse Support for broadcast communica9on No9fica9on can lead to further updates, causing a cascade effect 8

Learning goals for today Understand the design challenges and common solu9ons for Graphical User Interfaces (GUIs) Understand event- based programming Understand and recognize the design paverns used and how those design paverns achieve design goals. Observer pavern Strategy pavern Template method pavern Composite pavern Model- view- controller Other common GUI design paverns not discussed here: Decorator, Façade, Adapter, Command, Diagnose problems caused by computa9on in the GUI threads 9

Aside: Anonymous inner classes in Java You can implement an interface without naming the implemen9ng class E.g., public interface Runnable { public void run(); } public static void main(string[] args) { Runnable greeter = new Runnable() { public void run() { System.out.println("Hi mom!"); } }; } greeter.run(); 10

Scope within an anonymous inner class An anonymous inner class cannot access non- final variables in the scope where it is defined public interface Runnable { public void run(); } public static void main(string[] args) { String name = "Charlie"; Runnable greeter = new Runnable() { public void run() { System.out.println("Hi " + name); } }; } greeter.run(); compile-time error 11

Scope within an anonymous inner class An anonymous inner class cannot access non- final variables in the scope where it is defined public interface Runnable { public void run(); } public static void main(string[] args) { final String name = "Charlie"; Runnable greeter = new Runnable() { public void run() { System.out.println("Hi " + name); } }; } greeter.run(); OK 12

Today: Introduc9on to Graphical User Interfaces (GUIs) Event- based programming Building a GUI using Java Swing Event- handling and decoupling with the Observer pavern GUI design challenges Design paverns that solve them Prac9cal advice: Threading architecture of a typical GUI 13

Event- based programming Style of programming where control- flow is driven by (usually external) events public void performaction(actionevent e) { List<String> lst = Arrays.asList(bar); foo.peek(42) } public void performaction(actionevent e) { bigbloatedpowerpointfunction(e); withanamesolongimadeittwomethods(e); yesiknowjavadoesntworklikethat(e); } public void performaction(actionevent e) { List<String> lst = Arrays.asList(bar); foo.peek(40) } 14

Examples of events in GUIs 15

Examples of events in GUIs User clicks a buvon, presses a key User selects an item from a list, an item from a menu, expands a tree Mouse hovers over a widget, focus changes Scrolling, mouse wheel turned Resizing a window, hiding a window Drag and drop A package arrives from a web service, connec9on drops, System shutdown, 16

Interac9on with command- line interfaces Scanner input = new Scanner(System.in); while (ques9ons.hasnext()) { Ques9on q = ques9on.next(); System.out.println(q.toString()); String answer = input.nextline(); q.respond(answer); } 17

GUIs without event- based programming while (true) { if (iskeydown( Alt+Q ) break; if (iskeydown( F1 ) openhelp(); if (ismousedown(10 ) startmovingwindow(); } 18

Event- based GUIs //sta9c public void main JFrame window = window.setdefaultcloseopera9on( WindowConstants.EXIT_ON_CLOSE); window.setvisible(true); //on add- buvon click: String email = emailfield.gettext(); emaillist.add(email); 19

Event- based GUIs //sta9c public void main JFrame window = window.setdefaultcloseopera9on( WindowConstants.EXIT_ON_CLOSE); window.setvisible(true); //on add- buvon click: String email //on = remove- buvon click: emailfield.gettext(); int pos = emaillist.add(email); emaillist.getselecteditem(); if (pos>=0) emaillist.delete(pos); 20

(Blocking) Interac9ons with users Game Dealer Player newgame addcards addcards getaction action blocking execution [action==hit] addcard 21

Interac9ons with users through events Do not block wai9ng for user response Instead, react to user events e.g.: Game Dealer Player newgame addcards addcards hit addcard 22

Programming an event- based GUI Typically use a GUI framework Register code (a.k.a. callbacks) to handle different events Opera9ng system / GUI framework detects events Determines which components are registered to handle the event and calls the event handlers Your code is idle un9l called to handle events Program exits by calling some exit method Applica3on event mouse, key, redraw, GUI Framework drawing commands get event next event OS 23

Programming an event- based GUI Setup phase Describe how the GUI window should look Use libraries for windows, widgets, and layout Embed specialized code for later use Register callbacks Execu9on Framework gets events from OS Raw events: mouse clicks, key presses, window becomes visible, etc. Framework processes events Click at 10,40: which widget? Resize window: what to re- layout and redraw? Triggers callback func9ons of corresponding widgets (if registered) Applica3on event mouse, key, redraw, GUI Framework get event OS drawing commands next event 24

Example: The AlarmWindow edu.cmu.cs.cs214.rec06.alarmclock.alarmwindow Creates a JFrame with a JPanel to go in it Creates a text label and a buvon Makes the window (and its contents) visible when the alarm goes off When the dismiss buvon is clicked, its event handler hides the window 25

Example: The CustomerManagementUI rec06.customerlist.gui.customermanagementui Creates a JFrame with a JPanel to go in it Makes the window (and its contents) visible...rec06.customerlist.gui.customermanagementpanel Creates numerous labels and text fields, a customeraddbuvon Registers an event handler for the customeraddbuvon When the customeraddbuvon is clicked, its event handler gets the text from the text fields and adds a customer to the list 26

GUI frameworks in Java AWT Na9ve widgets, only basic components, dated Swing Java rendering, rich components SWT + JFace Mixture of na9ve widgets and Java rendering; created for Eclipse for faster performance Others Apache Pivot, SwingX, JavaFX, Different in their specific designs, but similar overall strategies and concepts 27

Swing JFrame JPanel JButton JTextField 28

Swing has many widgets JLabel JBuVon JCheckBox JChoice JRadioBuVon JTextField JTextArea JList JScrollBar and more JFrame is the Swing Window JPanel (aka a pane) is the container to which you add your components (or other containers) 29

To create a simple Swing applica9on Make a Window (a JFrame) Make a container (a JPanel) Put it in the window Add components (BuVons, Boxes, etc.) to the container Use layouts to control posi9oning Set up observers (a.k.a. listeners) to respond to events Op9onally, write custom widgets with applica9on- specific display logic Set up the window to display the container Then wait for events to arrive 30

Reacting to events 31

Crea9ng a buvon //static public void main JFrame window = JPanel panel = new JPanel(); window.setcontentpane(panel); JButton button = new JButton( Click me ); button.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { System.out.println( Button clicked ); } }); panel.add(button); window.setvisible(true); 32

Crea9ng a buvon //static public void main JFrame window = JPanel panel = new JPanel(); window.setcontentpane(panel); panel to hold the button JButton button = new JButton( Click me ); button.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { System.out.println( Button clicked ); } }); panel.add(button); window.setvisible(true); 33

Ac9onListeners Listeners are objects with callback func9ons Listeners can be registered to handle events on widgets All registered widgets are called if event occurs interface ActionListener { void actionperformed(actionevent e); } class ActionEvent { int when; String actioncommand; int modifiers; Object source(); int id; } 34

Ac9onListeners Listeners are objects with callback func9ons Listeners can be registered to handle events on widgets All registered widgets are called if event occurs interface ActionListener { void actionperformed(actionevent e); } class ActionEvent { class AbstractButton extends JComponent int { when; String actioncommand; private List<ActionListener> listeners; int modifiers; public void addactionlistener(actionlistener l) { Object source(); listeners.add(l); int id; } protected void fireactionperformed(actionevent } e) { for (ActionListener l: listeners) l.actionperformed(e); } } 35

Ac9onListeners What design pattern is this? Listeners are objects with callback func9ons Listeners can be registered to handle events on widgets All registered widgets are called if event occurs interface ActionListener { void actionperformed(actionevent e); } class ActionEvent { class AbstractButton extends JComponent int { when; String actioncommand; private List<ActionListener> listeners; int modifiers; public void addactionlistener(actionlistener l) { Object source(); listeners.add(l); int id; } protected void fireactionperformed(actionevent } e) { for (ActionListener l: listeners) l.actionperformed(e); } } 36

Recall the observer design pavern 37

Design discussion BuVon implementa9on should be reusable but customizable Different buvon label, different event- handling Must decouple buvon's ac9on from the buvon itself Listeners are separate independent objects A single buvon can have mul9ple listeners Mul9ple buvons can share the same listener 38

Swing has many event listener interfaces: Ac9onListener AdjustmentListener FocusListener ItemListener KeyListener MouseListener TreeExpansionListener TextListener WindowListener and on and on interface ActionListener { void actionperformed(actionevent e); } class ActionEvent { int when; String actioncommand; int modifiers; Object source(); 39

Decoupling from a GUI 40

A GUI design challenge Consider the transit simulator, implemented by a World class: Simulator World stores all en99es GUI shows en99es, triggers new events in simulator When should the GUI update the screen? World GUI step getdata update 41

A GUI design challenge, part 2 What if we add a warning to alert if a bus has moved? World GUI Warning step getdata update update getdata update 42

A GUI design challenge, part 3 What if the simulator world changes for reasons not caused by the GUI? World GUI Warning step getdata update update getdata update 43

A GUI design challenge, part 3: one possible design Let the World tell the GUI that something happened World GUI Warning step update(data) update(data) update update 44

A GUI design challenge, part 3: one possible design Let the World tell the GUI that something happened World GUI Warning step update(data) update(data) update update Problem: This couples the World to the GUI implementa9on. 45

Core implementa9on vs. GUI Core implementa9on: Applica9on logic Compu9ng some result, upda9ng data GUI Graphical representa9on of data Source of user interac9ons Design guideline: Avoid coupling the GUI with core applica;on Mul9ple UIs with single core implementa9on Test core without UI Design for change, design for reuse, design for division of labor; low coupling, high cohesion 46

A GUI design challenge, part 3: one possible design Let the World tell the GUI that something happened World GUI Warning step update(data) update(data) update update Problem: This couples the World to the GUI implementa9on. 47

Decoupling with the Observer pavern Let the world tell all interested components about updates World GUI Warning register register step notify notify update update 48

An architectural pavern: Model- View- Controller (MVC) Manage inputs from user: mouse, keyboard, menu, etc. Manage display of information on the screen Manage data related to the application domain 49

Model- View- Controller (MVC) Passive model Active model http://msdn.microsoft.com/en-us/library/ff649643.aspx 50

Separa9ng applica9on core and GUI, a summary Reduce coupling: do not allow core to depend on UI Create and test the core without a GUI Use the Observer pavern to communicate informa9on from the core (Model) to the GUI (View) GUI Tests GUI Core Tests Core 51

More GUI design challenges 52

Swing layouts The simplest, and default, layout. Wraps around when out of space. Like FlowLayout, but no wrappin More sophisticated layout managers see http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html 53

A naïve hard- coded implementa9on class JPanel { protected void dolayout() { switch(getlayouttype()) { case BOX_LAYOUT: adjustsizebox(); break; case BORDER_LAYOUT: adjustsizeborder(); break;... } } private adjustsizebox() { } } A new layout would require changing or overriding JPanel 54

A bever solu9on: delegate the layout responsibili9es Layout classes, e.g.: contentpane.setlayout(new FlowLayout()); contentpane.setlayout(new GridLayout(4,2)); Similarly, there are border classes to draw the borders, e.g.: contentpane.setborder(new EmptyBorder(5, 5, 5, 5)); What design pattern is this? 55

Recall the strategy pavern 56

Another GUI design challenge: nes9ng containers A JFrame contains a JPanel, which contains a JPanel (and/or other widgets), which contains a JPanel (and/or other widgets), which contains What design pattern best models this? 57

Recall the composite design pavern 58

Yet another GUI design challenge: par9al customiza9on JComponent. 59

Recall the template method pattern Applicability When an algorithm consists of varying and invariant parts that must be customized When common behavior in subclasses should be factored and localized to avoid code duplica9on To control subclass extensions to specific opera9ons Consequences Code reuse Inverted Hollywood control: don t call us, we ll call you Ensures the invariant parts of the algorithm are not changed by subclasses 60

The Swing threading architecture main() thread Create window Set up callbacks Show window (thread ends) GUI thread Loop forever: Get system event Invoke callback e.g. callback code: Run complex numeric algorthim (Problem: UI is unresponsive) Show result 61

The Swing threading architecture: worker threads main() thread Create window Set up callbacks Show window (thread ends) GUI thread Loop forever: Get system event Invoke callback Callback code: create SwingWorker start it execu9ng Worker thread Worker thread execu9on: invoke doinbackground() run complex numeric algorithm store result in SwingWorker signal to UI that we are done SwingWorker result : Long 62

Summary GUIs are full of design paverns Strategy pavern Template Method pavern Composite pavern Observer pavern Decorator pavern Façade pavern Adapter pavern Command pavern Model- View- Controller Swing for Java GUIs Separa9on of GUI and Core 63