GUI- Model-View-Controller

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interfaces
Graphical User Interfaces (Part IV)
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
The Model-View Approach in Java. OK, you’ve got “Hello World” running... What now?
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
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.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Io package as Java’s basic I/O system continue’d.
Ranga Rodrigo. Class is central to object oriented programming.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 19 Designing the GUI front-end: the Model-View-Controller pattern.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Event Handling Mohanraj S AP / IT Angel College of Engg & Tech.,
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Layout Managers Arranges and lays out the GUI components on a container.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport.
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.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
© 2006 Pearson EducationDesign Patterns1 of 20 A Final Example: A Traffic Light Let’s model a traffic light! – here’s the spec: Have a column of two circles.
Computer Science 209 GUIs Model/View/Controller Layouts.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
Architectural Patterns Support Lecture Patterns Pattern: A representation of a proven solution. Problem Applicable Forces Solution Consequences Benefits.
A cannon game ?. Simple version angle from command line, one shot only Coordinate system is “upside-down”: Use dy(int) method to transform y coordinate:
Creating a GUI Class An example of class design using inheritance and interfaces.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Modular Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
“Form Ever Follows Function” Louis Henri Sullivan
GUIs Model/View/Controller Layouts
CHAPTER 7 & 8 REVIEW QUESTIONS
Architectural Patterns for Interactive Software
Ellen Walker Hiram College
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Timer class and inner classes
Introduction to Event Handling
Constructors, GUI’s(Using Swing) and ActionListner
Programming Graphical User Interface (GUI)
GUI building with the AWT
static public void graficar(
Graphical User Interface
GUI- Model-View-Controller
Presentation transcript:

GUI- Model-View-Controller CSI 1101 N. El Kadri

Model-View-Controller (MVC) pattern Makes it easier to modify or customize each part; Designed to allow for multiple views of the same data; Binding between the model and the view is done at runtime (rather than compile time), i.e. views can be changed dynamically; The API of each component is well defined, the model, the controller or the view can be modified/replaced easily; Allows to focus on each aspect of the application independently.

MVC Model – the implementation, state: attributes and behaviour; View – the outgoing interface, a representation of the model to the outside world; Controller – the incoming interface, forwards the requests to update the model.

Model Contains the data of the program and the methods to transform the data; Has no knowledge of either the views or the controllers (sometimes not even a reference); The View can greatly differ from the Model.

View(s) A view is an object that presents a visual display of the data for the user.

Controller A controller is an object that provides the means for the user to change the model or its visual representation. Knows what can be done to the model, for example, adding or removing an entry.

Example 1 As our first example, let’s design a graphical user interface for the class Counter.

public class Counter { private int value; public Counter() { value = 0; } public void increment() { value++; public int getValue() { return value; public String toString() { return Integer.toString( value );

To allow for the independent development of the UI, to facilitate the maintenance of the application and to allow for replacement views, let us create an interface to characterize the interactions between the model an its views, CounterView. public interface CounterView { public void update(); } The only knowledge the model has about its view is that a view has a method called update.

Changes to Counter A new instance variable to reference its view: private CounterView view; An instance method to set its view: public void setView(CounterView v) { view = v; }

Changes to Counter (contd) increment is changed so that whenever the state of the Counter changes the view is notified: public void increment() { value++; view.update(); }

public class Counter { private int value; private CounterView view; public Counter() { value = 0; } public void increment() { value++; view.update(); public int getValue() { return value; public void setView( CounterView v ) { view = v; public String toString() { return Integer.toString( value );

Controller public class Controller implements ActionListener { private Counter model; public Controller( Counter m ) { model = m; } public void actionPerformed( ActionEvent e ) { model.increment();

View public class View extends Frame implements CounterView { private Controller controller; private Counter model; private Label value; private Button update = new Button( "Update" ); public View( Counter model ) { super( "Counter UI" ); this.model = model; controller = new Controller( model ); setLayout( new GridLayout( 2,1 ) ); value = new Label( model.toString() ); add( value ); add( update ); update.addActionListener( controller ); addWindowListener( new WindowClosingHandler() ); pack(); show(); }

View - cont’d public void update() { value.setText( model.toString() ); } private class WindowClosingHandler extends WindowAdapter { public void windowClosing( WindowEvent e ) { System.exit (0); } // end of View

The main program consists of: public class Run { public static void main( String[] args ) { Counter model = new Counter(); View view = new View( model ); model.setView( view ); }

Another example Let’s consider a more complex, example that makes use of a dialog to enter new data. This example uses a simple implementation of a Stop-Watch based on a fixed size array — since the array has a fixed size, its method addTime returns false when the array is full. StopWatch is the Model! For this particular implementation, the model has no knowledge of the user interface at all (no reference to it). The controller and view are implemented by Stop-WatchUI (sometimes the Controller and the View are implemented by the same object).

public class StopWatch { public static final int CAPACITY = 15; private Time[] times = new Time[ CAPACITY ]; private int last = 0; public boolean addTime( Time t ) { boolean success; if ( last == times.length ) success = false; else { times[ last ] = t; last++; success = true; } return success; public Time timeAt( int pos ) { return times[ pos ]; public int size() { return last;

public class StopWatchApplication { public static void main( String args[] ) { StopWatch model = new StopWatch(); StopWatchUI ui = new StopWatchUI( model ); }

import java.awt.*; import java.awt.event.*; public class StopWatchUI extends Frame { // model protected StopWatch model; // view private Button bTime = new Button( "Add New Time" ); private Button bQuit = new Button( "Quit" ); private List output = new List( 10 ); private Dialog dTime = new TimeDialog( this ); // ...

// Constructor public StopWatchUI( StopWatch model ) { super( "StopWatch UI" ); this.model = model; addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit(0); } }); bTime.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { dTime.setVisible( true ); bQuit.addActionListener( new ActionListener() { public void actionPerformed ( ActionEvent e ) { System.exit( 0 );

} // ... setLayout( new BorderLayout() ); add( output, "Center" ); Panel bottom = new Panel(); bottom.setLayout( new GridLayout( 1,2 ) ); bottom.add( bTime ); bottom.add( bQuit ); add( bottom, "South" ); pack(); show(); } // ...

public void displayTimes() { output.removeAll(); for ( int i=0; i<model.size(); i++ ) { output.add( model.timeAt( i ).toString() ); output.makeVisible( i ); } public void addTime( Time t ) { if ( ! model.addTime( t ) ) new ErrorDialog( this, "Maximum Capacity Exceeded" ); else displayTimes();

import java.awt.*; import java.awt.event.*; class TimeDialog extends Dialog { private StopWatchUI parent; private IntField inHours = new IntField(); private IntField inMinutes = new IntField(); private IntField inSeconds = new IntField(); private Button bCreate = new Button("Create"); private Button bCancel = new Button("Cancel"); // ...

// Constructor TimeDialog( StopWatchUI parent ) { super( parent, "Time Dialog" , true ); this.parent = parent; bCreate.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { addTime(); } }); bCancel.addActionListener( new ActionListener() { cancel();

setLayout( new GridLayout( 4,2 ) ); add( new Label( "Hours:" ) ); add( inHours ); add( new Label( "Minutes:" ) ); add( inMinutes ); add( new Label( "Seconds:" ) ); add( inSeconds ); add( bCreate ); add( bCancel ); pack(); } // ...

// instance methods private void addTime() { int h = inHours.getValue(); int m = inMinutes.getValue(); int s = inSeconds.getValue(); parent.addTime( new Time( h, m, s ) ); setVisible( false ); } private void cancel() { class IntField extends TextField { public int getValue() { return Integer.parseInt( getText() );

import java.awt.*; import java.awt.event.*; class ErrorDialog extends Dialog { private Button button; ErrorDialog(Frame parent, String message) { super(parent, "Error Dialog" , true); button = new Button( "Ok" ); button.addActionListener( new ActionListener () { public void actionPerformed( ActionEvent e ) { dispose(); } }); add( "Center", new Label( message ) ); add( "South", button ); pack(); setVisible( true );

Observer/Observable Two-way associations make classes inseparable. To avoid these interdependencies, classes are often structured as Observer and Observable. The Observer has an extensive knowledge of the Observable, but the Observable only knows that the Observer can be notified. Observer is an interface that has a method update. Observable is an abstract class, that has all the necessary methods to set, delete and notify multiple observers.