Event-driven Programming, Separation of Concerns, the Observer pattern and the JavaFX Event Infrastructure

Similar documents
Event Handling in JavaFX

CSE 331. Model/View Separation and Observer Pattern

Java Programming Lecture 7

Composite Pattern Diagram. Explanation. JavaFX Subclass Hierarchy, cont. JavaFX: Node. JavaFX Layout Classes. Top-Level Containers 10/12/2018

event driven programming user input Week 2 : c. JavaFX user input Programming of Interactive Systems


Outline. Design Patterns. Observer Pattern. Definitions & Classifications

CST141 JavaFX Events and Animation Page 1

Event-Driven Programming with GUIs. Slides derived (or copied) from slides created by Rick Mercer for CSc 335

CSE 331 Software Design and Implementation. Lecture 16 Callbacks and Observers

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

Observer Pattern. CS580 Advanced Software Engineering October 31, Yu Sun, Ph.D.

Java Event Handling -- 1

CSC 161 LAB 3-1 JAVA FX CALCULATOR

Week 12 Thursday. For milestone #2 and #3, also submit a screenshot of your GUI once it is launched.

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

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

CSE 331 Software Design and Implementation. Lecture 17 Events, Listeners, Callbacks

Real World. static methods and Console & JavaFX App Intros. Lecture 16. Go ahead and PULL Lecture Materials & Sign-in on PollEv

IT 313 Advanced Application Development

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

Distributed Collaboration - Assignment 1: Multi-View 1-User IM

Java Programming. Events and Listeners

are most specifically concerned with

Graphical User Interface (GUI)

MIT AITI Swing Event Model Lecture 17

Graphical User Interface (GUI)

Java. Error, Exception, and Event Handling. Error, exception and event handling. Error and exception handling in Java

2.6 Error, exception and event handling

Event Driven Programming

Introduction to concurrency and GUIs

Lecture 19 GUI Events

Java FX. Threads, Workers and Tasks

CSE 331 Software Design & Implementation

Window Interfaces Using Swing Objects

Case studies: Outline Case Study: Noughts and Crosses

Design Patterns Design patterns advantages:

CS 112 Programming 2. Lecture 14. Event-Driven Programming & Animations (1) Chapter 15 Event-Driven Programming and Animations

Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of

Event-driven Programming: GUIs

Model-View Controller IAT351

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

CS5010 PDP Introduction to Design. Maria Zontak. Slides inspired by slides of H.Perkins (UW, CS331) and MIT software engineering course

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

Jonathan Aldrich Charlie Garrod

Tecniche di Progettazione: Design Patterns

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

Solution register itself

02. OBSERVER PATTERN. Keep your Objects in the know. Don t miss out when something interesting happens

Window Interfaces Using Swing Objects

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

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

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

Observer pattern. Somebody s watching me...

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Outline. Observer Pattern: Pitfalls. Observer Applications

Building Graphical User Interfaces with the MVC Pattern

CSCI-142 Exam 2 Review September 25, 2016 Presented by the RIT Computer Science Community

Graphical User Interfaces. Comp 152

Chapter 15 Event-Driven Programming and Animations

SYSC Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

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

46 Advanced Java for Bioinformatics, WS 17/18, D. Huson, December 21, 2017

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

CS 116x Winter 2015 Craig S. Kaplan. Module 03 Graphical User Interfaces. Topics

Lecture 5. Lecture

SINGLE EVENT HANDLING

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

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

A reusable design concept

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

Design Patterns. it s about the Observer pattern, the Command pattern, MVC, and some GUI. some more

7. Program Frameworks

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common.

The Observer Pattern

CS112 Lecture: Reuse, Packages, Patterns

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

Beyond CSE143. What s Left To Do? Templates. Using Templates. A Template Class. A Problem with Reusing Code CSE 143

ITNP090 - Object Oriented Software Design

Lecture 4: UI Software Architecture. Fall UI Design and Implementation 1

Advanced Java for Bioinformatics Winter 2017/18. Prof. Daniel Huson

Asynchronous Programming

TO DO: Create a new class which adds statistics to the dice. NOTE: Don t forget to run regression tests

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

Charlie Garrod Michael Hilton

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Studying software design patterns is an effective way to learn from the experience of others

Module dependences and decoupling (Events, listeners, callbacks)

Example: not good design. Motivation for MVC. public class BankAccount extends JPanel implements ActionListener

Chapter 14. Exception Handling and Event Handling ISBN

Programming Language Concepts: Lecture 8

Microthread. An Object Behavioral Pattern for Managing Object Execution. 1.0 Intent. 2.0 Also Known As. 3.0 Classification. 4.0 Motivation/Example

Introducing Application Design and Software Engineering Principles in Introductory CS Courses: Model-View-Controller Java Application Framework

GUI Event Handlers (Part I)

implementation support

Part I: Learn Common Graphics Components

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

CS 251 Intermediate Programming GUIs: Event Listeners

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING Expression Tree Case Study : Prototype Pattern Reactor pattern

Transcription:

Java GUIs in JavaFX Event-driven Programming, Separation of Concerns, the Observer pattern and the JavaFX Event Infrastructure 1

GUIs process inputs and deliver outputs for a computing system Inputs Click a button to perform an operation Type keys to provide text or set values Move to a new focus (another control) Drag/select visual components Accept different inputs that mean the same Outputs Show a graph of a value over time Display text, graphics, images Present audio Display a system value in various ways 2

JavaFX Event Class Hierarchy A user stimulates the system through the GUI The GUI generates events for the stimulus The GUI forwards those events to handlers 3 Handlers fulfill the system function

javafx.event.eventhandler EventHandler<T extends Event> class The interface for handlers has one method. void handle(t event) It is therefore a functional interface. The class Control allows registering of EventHandlers void addeventhandler( ) We will stick to ActionEvents. setonaction( EventHandler<ActionEvent> eh) For Button, ChoiceBox, TextField,... 4

Adding and Removing a Handler Also Called 'registering' and 'unregistering' Methods are found in javafx.scene.node JavaFX GUI objects interact with each other To add a handler (advanced): addeventhandler( EventType<T> eventtype, EventHandler<? super T> eventhandler) To remove a handler: 5 removeeventhandler( EventType<T> eventtype, EventHandler<? super T> eventhandler)

Division of Responsibility GUI controls accept input stimuli But should a Button do the work to respond? A Button won't then be reusable GUI views display system values/state What decides how to show that state? A value could be shown as text or a graphic or both at different times or for different audiences 6

System Design Divide a system into components Modular design tells us this Assign tasks to components to organize it Divide and conquer! A component should do one thing well Focus like a laser on its job Then make the components communicate That means the GUI must tell other components what the user wants, and display to the user what is happening elsewhere. 7

Separation of Concerns Goal: Keep IO and Operation domains separate External Input/Output Domain Domain of Operation Show Act Upon GUI Component System Component User or other system 8 Events happen between components here Provide support for value displays and input entry And here Provide application response to input and output delivery

Example External Input/Output Domain Domain of Operation See text Press button Button( Take Action ) ok listener or event handler that was added to button EventHandler< ActionEvent> User or other system handle( ActionEvent ) { System.out.println( ok ); } 9

Subject-Observer Pattern Closely related to event handling is the Observer Pattern. A Subject sets itself up to be 'seen' by other objects the Observers that want to view An Observer calls the subject to add itself as a listener for changes to the subject 12 The subject notifies its observers when the subject changes state by calling a standard method update() on its observers Observer's update decides how to respond Decouple the subject from details of observer.

Observer Pattern 13 https://sourcemaking.com/design_patterns/observer [10/2017]

Updates and Notifications We can write Observer code (observer) that is told when a GUI event happens. Button clicked, call the observers (handlers). But remember the GUI has no observable state. The true Observer case is when the state of the actual application (model) changes Change can be internally/externally caused In this case, the Observable (subject) needs to tell the GUI that something changed source? 14 mutating action Application Base Model update GUI Component (observer)

Observer Pattern Java's Observer pattern is in java.util The Observer interface: void update(observable o, Object arg) extra data passed from observable to observers Major methods of the Observable class: 15 void addobserver(observer o) void notifyobservers() void notifyobservers(object arg) void setchanged()...

Model-View-Controller: Decoupling a GUI from its Subject A GUI presents information to the 16 outside world May change at different rate from system A GUI's Input processing feeds data and commands into the system Needs an interface to operate through The Subject system operates on input and produces output Does not want to tie tightly to any one display because there may be different evolving needs Provides an interface for feeding data and commands in and sending results out Evolves separately from the view and the control of input

Model-View-Controller: Decoupling a GUI from its Subject View VIEW Components Major boundary is between the problem domain and the user interface. Model defines the connection points. Physical Action Model uses abstract interfaces. Control should provide visual feedback. For example a button shows a visible response to a click. MODEL Components 17 CONTROL Components Control and View interact with the model through a specific problem interface.

notify Sequence of Calls in CounterGUI Button event handler GUI Application TextField Counter(model) push (inside JavaFX) increment update getcount settext 18 Note that this all happens on one thread!

Java Event Thread The thread that runs the event handling on the previous slide is an event thread created by Application. No other thread may make updates (like settext) on GUI elements created by the Application subclass. If that has to happen, queue up your update by calling JavaFX's runlater method: Platform.runLater( () mylabel.settext( ) ); 19

Model-View-Controller: Choices for implementation Use a classic console interface. Still the best choice for some types of work. Really. Use Swing and AWT event handling This is a legacy approach, still often used. JavaFX: several approaches Java general-utility tools java.util.observer and java.util.observable Javafx tools and approaches EventHandler and EventFilter javafx.beans properties sophisticated, but complicated to learn 20