Based on slides by Prof. Burton Ma

Similar documents
Inheritance (cont) Abstract Classes

Implementing Graphical User Interfaces

SampleApp.java. Page 1

Model-View-Controller (MVC) Structure Previous - Presentation-Model

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects

AP CS Unit 11: Graphics and Events

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

MVC: Model View Controller

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

MIT AITI Swing Event Model Lecture 17

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

Graphical User Interface

PART 23. Java GUI Advanced JList Component. more items.

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Interfaces. consider an interface for mathematical functions of the form

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

Building Graphical User Interfaces. GUI Principles

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CMP 326 Midterm Fall 2015

PART1: Choose the correct answer and write it on the answer sheet:

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Building Graphical User Interfaces. Overview

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CS 180 Final Exam Review 12/(11, 12)/08

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

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

Graphical User Interfaces. Comp 152

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

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

Example: Building a Java GUI

Example: Building a Java GUI

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

COMP16121 Sample Code Lecture 1

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

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

Today. cisc3120-fall2012-parsons-lectiii.3 2

Answer on question #61311, Programming & Computer Science / Java

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

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

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

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Swing from A to Z Some Simple Components. Preface

A Simple Text Editor Application

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

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

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

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

GUI Forms and Events, Part II

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

We are on the GUI fast track path

Window Interfaces Using Swing. Chapter 12

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

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

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

Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.


Programming graphics

DAFTAR LAMPIRAN. Source Code Java Aplikasi Keyword to Image Renamer Split

Attempt FOUR questions Marking Scheme Time: 120 mins

II 12, JFileChooser. , 2. SolidEllipse ( 2), PolyLine.java ( 3). Draw.java

Contents Chapter 1 Introduction to Programming and the Java Language

Graphical User Interface (Part-3) Supplementary Material for CPSC 233

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Graphical User Interface (GUI)

Java Interfaces Part 1 - Events Version 1.1

Class 16: The Swing Event Model

Section Basic graphics

import java.applet.applet; import java.applet.audioclip; import java.net.url; public class Vjesala2 {

Final Examination Semester 3 / Year 2008

Programming Languages 2nd edition Tucker and Noonan"

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

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

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

* To change this license header, choose License Headers in Project Properties.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

More about GUIs GEEN163

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!

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

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

1.00 Lecture 31. Streams 2. Reading for next time: Big Java , The 3 Flavors of Streams

CMP 326 Final Spring There is a blank page at the end of the exam if you need more room to answer a question.

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

Dr. Hikmat A. M. AbdelJaber

Original GUIs. IntroGUI 1

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

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

Events and Exceptions

Lampiran A. SOURCE CODE PROGRAM

Graphical User Interfaces in Java - SWING

Workbook 7. Remember to check the course website regularly for announcements and errata:

SINGLE EVENT HANDLING

Transcription:

Based on slides by Prof. Burton Ma 1

TV - on : boolean - channel : int - volume : int + power(boolean) : void + channel(int) : void + volume(int) : void Model View Controller RemoteControl + togglepower() : void + channelup() : void + volumeup() : void 2

http://java.sun.com/developer/technicalarticles/javase/mvc/ 3

Model Represents state of the application and the rules that govern access to and updates of state View Presents the user with a sensory (visual, audio, haptic) representation of the model state A user interface element (the user interface for simple applications) Controller Processes and responds to events (such as user actions) from the view and translates them to model method calls 4

A simple application that lets the user roll a die When the user clicks the Roll button the die is rolled to a new random value Event driven programming 5

This application is simple enough to write as a single class SimpleRoll.java 6

We can also write the application using the model-view-controller pattern SimpleModel.java SimpleView.java SimpleController.java SimpleApp.java 7

Implement a simple calculator using the model-view-controller (MVC) design pattern Features: Sum, subtract, multiply, divide Clear Records a log of the user actions Save the log to file Read the log from a file 8

9

The calculator application is launched by the user The text refers to the application as the GUI The application: 1. Creates the model for the calculator, and then 2. Creates the view of the calculator 10

public class CalcMVC public static void main(string[] args) CalcModel model = new CalcModel(); CalcView view = new CalcView(model); view.setvisible(true); 11

Features: Sum, subtract, multiply, divide Clear Records a log of the user actions Save the log to file Read the log from a file 12

BigInteger: Immutable arbitrary-precision integers CalcModel - calcvalue : BigInteger - log : ArrayList<String> + getcalcvalue() : BigInteger + getlastuservalue() : BigInteger + sum(biginteger) : void + subtract(biginteger) : void + multiply(biginteger) : void + divide(biginteger) : void + clear() : void + save(file) : void + open(file) : void - updatelog(string operation, String uservalue) : void 13

public class CalcModel private BigInteger calcvalue; private ArrayList<String> log; // creates the log and initializes the attributes // using the clear method CalcModel() this.log = new ArrayList<String>(); this.clear(); 14

// sets the calculated value to zero, clears the log, // and adds zero to the log public void clear() this.calcvalue = BigInteger.ZERO; this.log.clear(); this.log.add(this.calcvalue.tostring()); 15

// empty log looks like // [0] // non-empty log looks like: // [0, +, 5, =, 5, -, 3, =, 2, *, 7, =, 14] public BigInteger getlastuservalue() if(this.log.size() == 1) return BigInteger.ZERO; final int last = this.log.size() - 1; return new BigInteger(this.log.get(last - 2)); 16

// BigInteger is immutable; no privacy leak public BigInteger getcalcvalue() return this.calcvalue; 17

// sums the user value with the current calculated value // and updates the log using updatelog public void sum(biginteger uservalue) this.calcvalue = this.calcvalue.add(uservalue); this.updatelog("+", uservalue.tostring()); 18

public void subtract(biginteger uservalue) this.calcvalue = this.calcvalue.subtract(uservalue); this.updatelog("-", uservalue.tostring()); public void multiply(biginteger uservalue) this.calcvalue = this.calcvalue.multiply(uservalue); this.updatelog("*", uservalue.tostring()); 19

// cannot divide by zero; options: // 1. precondition uservalue!= 0 // 2. validate uservalue; do nothing // 3. validate uservalue; return false // 4. validate uservalue; throw exception public void divide(biginteger uservalue) this.calcvalue = this.calcvalue.divide(uservalue); this.updatelog("/", uservalue.tostring()); 20

// relies on fact ArrayList implements Serializable public void save(file file) FileOutputStream f = null; ObjectOutputStream out = null; try f = new FileOutputStream(file); // can throw out = new ObjectOutputStream(f); // can throw out.writeobject(this.log); // can throw out.close(); catch(ioexception ex) 21

public void open(file file) FileInputStream f = null; ObjectInputStream in = null; ArrayList<String> log = null; // object to read from file try f = new FileInputStream(file); // can throw in = new ObjectInputStream(f); // can throw log = (ArrayList<String>) in.readobject(); // can throw in.close(); this.log = log; final int last = this.log.size() - 1; this.calcvalue = new BigInteger(this.log.get(last)); catch(ioexception ex) catch(classnotfoundexception ex) 22

View Presents the user with a sensory (visual, audio, haptic) representation of the model state A user interface element (the user interface for simple applications) 23

Simple applications often consist of just a single window (containing some controls) JFrame window with border, title, buttons 24

A View can be implemented as a subclass of a JFrame Hundreds of inherited methods but only a dozen or so are commonly called by the implementer (see URL below) Object Component Container Window Frame JFrame View user interface item holds other components plain window window with title and border http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html 25

The View is responsible for creating: The Controller All of the user interface (UI) components menus JMenuBar, JMenu, JMenuItem buttons JButton labels JLabel text fields JTextField file dialog JFileChooser The View is also responsible for setting up the communication of UI events to the Controller Each UI component needs to know what object it should send its events to 26

public class CalcView extends JFrame public CalcView(CalcModel model) super("simple Calculator"); model.clear(); CalcController controller = new CalcController(model, this); this.setdefaultcloseoperation(jframe.exit_on_close); 27

a menu appears in a menu bar (or a popup menu) each item in the menu is a menu item menu JMenu menu bar JMenuBar menu item JMenuItem JMenuBar + add(jmenu) * JMenu * + add(jmenuitem) JMenuItem http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html 28

To create a menu Create a JMenuBar Create one or more JMenu objects Add the JMenu objects to the JMenuBar Create one or more JMenuItem objectes Add the JMenuItem objects to the JMenu 29

JMenuBar menubar = new JMenuBar(); JMenu filemenu = new JMenu("File"); menubar.add(filemenu); JMenuItem printmenuitem = new JMenuItem("Print"); filemenu.add(printmenuitem); 30

import javax.swing.jframe; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; public class CalcView extends JFrame private JMenuBar menubar; private JMenu filemenu; public CalcView(CalcModel model) super("simple Calculator"); model.clear(); CalcController controller = new CalcController(model, this); this.menubar = new JMenuBar(); thi tjm B (thi B ) 31

A label displays unselectable text and images A text field is a single line of editable text The ability to edit the text can be turned on and off label JLabel text field (edit off) JTextField label JLabel text field (edit on) JTextField http://docs.oracle.com/javase/tutorial/uiswing/components/label.html http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html 32

To create a label JLabel label = new JLabel("text for the label"); To create a text field (20 characters wide) JTextField textfield = new JTextField(20); 33

import java.awt.flowlayout; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.jtextfield; public class CalcView extends JFrame private JMenuBar menubar; private JMenu filemenu; private JTextField calctext; private JTextField uservaluetext; public CalcView(CalcModel model) ("Si l C l l t ") 34

A button responds to the user pointing and clicking the mouse on it (or the user pressing the Enter key when the button has the focus) button JButton http://docs.oracle.com/javase/tutorial/uiswing/components/button.html 35

To create a button JButton button = new JButton("text for the button"); 36

import java.awt.flowlayout; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.jtextfield; public class CalcView extends JFrame private JMenuBar menubar; private JMenu filemenu; private JTextField calctext; private JTextField uservaluetext; private JButton sumbutton; private JButton subtractbutton; i t JB tt lti l B tt 37

A file chooser provides a GUI for selecting a file to open (read) or save (write) file chooser (for choosing a file to open) JFileChooser http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html 38

import java.awt.flowlayout; import java.io.file; import javax.swing.jbutton; import javax.swing.jfilechooser; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.jtextfield; public class CalcView extends JFrame private JMenuBar menubar; private JMenu filemenu; private JTextField calctext; private JTextField uservaluetext; i t JB tt B tt 39

So far we have a View with some UI elements (buttons, text fields, menu items) Now we need to implement the actions Each UI element is a source of events Button pressed, slider moved, text changed (text field), etc. When the user interacts with a UI element an event is triggered This causes an event object to be sent to every object listening for that particular event The event object carries information about the event The event listeners respond to the event 40

event listener A event source 1 event object 1 event listener B event listener C event source 2 event object 2 event listener D 41

"open" "save" event object "open" "sum" "subtract" Controller "multiply" "divide" "clear" AbstractButton event object "clear" ActionEvent implements ActionListener 42

Each JButton and JMenuItem has two inherited methods from AbstractButton public void addactionlistener(actionlistener l) public void setactioncommand(string actioncommand) For each JButton and JMenuItem 1. Call addactionlistener with the controller as the argument 2. Call setactioncommand with a string describing what event has occurred 43

import java.awt.flowlayout; import java.awt.event.actionlistener; import java.io.file; import javax.swing.jbutton; import javax.swing.jfilechooser; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.jtextfield; public class CalcView extends JFrame private JMenuBar menubar; private JMenu filemenu; private JTextField calctext; i t JT tfi ld V l T t 44

Controller Processes and responds to events (such as user actions) from the view and translates them to model method calls Needs to interact with both the view and the model but does not own the view or model Aggregation View is a subclass of JFrame JFrame View 1 Controller 1 Model Controller has 1 View Controller has 1 Model 45

import java.awt.event.actionlistener; public class CalcController implements ActionListener private CalcModel model; private CalcView view; public CalcController(CalcModel model, CalcView view) this.model = model; this.view = view; 46

Tecall that our application only uses events that are fired by buttons (JButtons and JMenuItems) A button fires an ActionEvent event whenever it is clicked CalcController listens for fired ActionEvents How? by implementing the ActionListener interface public interface ActionListener void actionperformed(actionevent e); 47

CalcController was registered to listen for ActionEvents fired by the various buttons in CalcView (see method setcommand in CalcView) Whenever a button fires an event, it passes an ActionEvent object to CalcController via the actionperformed method actionperformed is responsible for dealing with the different actions (open, save, sum, etc) 48

1 actionperformed CalcView getopenfile 2 setuservalue 5 setcalcvalue 7 CalcController 3 open 4 getlastuservalue 6 getcalcvalue CalcModel 49

import java.awt.event.actionlistener; public class CalcController implements ActionListener private CalcModel model; private CalcView view; public CalcController(CalcModel model, CalcView view) this.model = model; this.view = view; /** * Invoked when an event occurs. * * @param event * The event. */ 50

1 actionperformed CalcView getsavefile 2 CalcController 3 save CalcModel 51

import java.awt.event.actionlistener; public class CalcController implements ActionListener private CalcModel model; private CalcView view; public CalcController(CalcModel model, CalcView view) this.model = model; this.view = view; /** * Invoked when an event occurs. * * @param event * The event. */ 52

1 actionperformed CalcView getuservalue 2 setcalcvalue 5 CalcController 3 sum 4 getcalcvalue CalcModel 53

import java.awt.event.actionlistener; public class CalcController implements ActionListener private CalcModel model; private CalcView view; public CalcController(CalcModel model, CalcView view) this.model = model; this.view = view; /** * Invoked when an event occurs. * * @param event * The event. */ 54

Source code for the model, view, controller, and app can be found on the course web site with the lectures 55

Even with only 5 buttons and 2 menu items our actionperformed method is unwieldy Imagine what would happen if you tried to implement a Controller this way for a big application Rather than one big actionperformed method we can register a different ActionListener for each button Each ActionListener will be an object that has its own version of the actionperformed method 56

ArithmeticListener SumListener SubtractListener DivideListener 57

Whenever a listener receives an event corresponding to an arithmetic operation it does: 1. Asks CalcView for the user value and converts it to a BigInteger getuservalue method 2. Asks CalcModel to perform the arithmetic operation dooperation method 3. Updates the calculated value in CalcView 58

private abstract class ArithmeticListener implements ActionListener 1. 2. 3. @Override public void actionperformed(actionevent action) BigInteger uservalue = this.getuservalue(); if (uservalue!= null) this.dooperation(uservalue); this.setcalculatedvalue(); 59

/** * Subclasses will override this method to add, subtract, * divide, multiply, etc., the uservalue with the current * calculated value. */ protected abstract void dooperation(biginteger uservalue); 60

private BigInteger getuservalue() BigInteger uservalue = null; try uservalue = new BigInteger(getView().getUserValue()); catch(numberformatexception ex) return uservalue; Note: these methods need access to the view and model which are associated with the controller. private void setcalculatedvalue() getview().setcalcvalue(getmodel().getcalcvalue(). tostring()); 61

How do we give the listeners access to the view and model? Could use aggregation Alternatively, we can make the listeners be inner classes of the controller 62

An inner class is a (non-static) class that is defined inside of another class public class Outer // Outer's attributes and methods private class Inner // Inner's attributes and methods 63

An inner class has access to the attributes and methods of its enclosing class, even the private ones public class Outer private int outerint; private class Inner public setouterint(int num) outerint = num; note not this.outerint use Outer.this.outerInt 64

public class CalcController2 //... // inner class of CalcController2 private abstract class ArithmeticListener implements ActionListener //... // inner class of CalcController2 private class SumListener extends ArithmeticListener @Override protected void dooperation(biginteger uservalue) //... 65

private class SumListener extends ArithmeticListener @Override protected void dooperation(biginteger uservalue) if (uservalue!= null) getmodel().sum(uservalue); 66

Only the controller needs to create instances of the various listeners I.e., the listeners are not useful outside of the controller Making the listeners private inner classes ensures that only CalcController can instantiate the listeners The listeners need access to private methods inside of CalcController (namely getview and getmodel) Inner classes can access private methods 67