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

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

Come organizzare gli ascoltatori/osservatori

Posizionamento automa-co: Layouts di base. h5p://docs.oracle.com/javafx/2/ layout/jfxpub- layout.htm

GUI Output. Adapted from slides by Michelle Strout with some slides from Rick Mercer. CSc 210

Essential JavaFX. Using layouts. Panes in JavaFX. Layouts. Tobias Andersson Gidlund LAYOUTS

Java Foundations. 9-1 Introduction to JavaFX. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

C30c: Model-View-Controller and Writing Larger JavaFX Apps

JavaFX a Crash Course. Tecniche di Programmazione A.A. 2016/2017

Multimedia-Programmierung Übung 3

JavaFX a Crash Course. Tecniche di Programmazione A.A. 2015/2016

//Create BorderPane layout manager. layout = new BorderPane(); //This is the "root node".

Java Programming. Events and Listeners

interactive systems graphical interfaces Week 2 : a. Intro to JavaFX Programming of Interactive Systems

Event-Driven Programming

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


Java FX. Properties and Bindings

Java FX. Threads, Workers and Tasks

JavaFX Basics. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.

IT In the News. Login tokens have been reset for those affected and vulnerabilities have been fixed. o Vulnerabilities existed since July 2017

CST141 JavaFX Events and Animation Page 1

CSC 161 LAB 3-1 JAVA FX CALCULATOR

CSE 331 Software Design & Implementation

CON Visualising GC with JavaFX Ben Evans James Gough

Chapter 14 JavaFX Basics. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Lecture 19 GUI Events

C12: JavaFX Scene Graph, Events, and UI Components

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

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

JavaFX a Crash Course. Tecniche di Programmazione A.A. 2017/2018

Event Handling in JavaFX

Java Programming Layout

canoo Engineering AG

PART ONE CREATING THE BOARD AND MAIN SPRITES

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

c 2017 All rights reserved. This work may be distributed or shared at no cost, but may not be modified.

JavaFX fundamentals. Tecniche di Programmazione A.A. 2012/2013

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

Introduction: Game. Key Design Points

Beautiful User Interfaces with JavaFX

JavaFX:Using Built-in Layout Panes

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

CST141 JavaFX Basics Page 1

C14: JavaFX: Overview and Programming User Interface

COMP6700/2140 Scene Graph, Layout and Styles

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

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

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

1. (5 points) In your own words, describe what an instance is.

Chapter 15 Event-Driven Programming and Animations

Throughout the exam, write concisely and underline key words or phrases. Have fun! Exam 2. Week 7 (Winter 2013). Dr. Yoder. Sec 031.

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

C16a: Model-View-Controller and JavaFX Styling

JavaFX. Embedding Swing Content in JavaFX Applications Release 8.0 Developer Preview E

Building Graphical user interface using JavaFX

Chapter 15. Event-Driven Programming and Animations

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

COMP1406 Tutorial 5. Objectives: Getting Started: Tutorial Problems:

GUI Event Handlers (Part I)

Grafica e non solo: Java FX

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

JavaFX Application Structure. Tecniche di Programmazione A.A. 2017/2018

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

Final Assignment for CS-0401

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

JavaFX. Working with Layouts in JavaFX Release 8 E

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

CS-202 Introduction to Object Oriented Programming

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

JavaFX. Getting Started with JavaFX Scene Builder Release 1.1 E

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

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

Window Interfaces Using Swing Objects

JavaFX. JavaFX Scene Builder Release Notes Release 2.0 Early Access E

2IS45 Programming

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

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

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

Event-driven Programming: GUIs

Computational Expression

Windows and Events. created originally by Brian Bailey

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

Window Interfaces Using Swing Objects

Event Driven Programming

Jonathan Aldrich Charlie Garrod

SampleApp.java. Page 1

JavaFX. Working with the JavaFX Scene Graph Release 8 E March 2014 Learn about the concept of a scene graph and how it is used in JavaFX.

Graphical User Interfaces. Comp 152

Part I: Learn Common Graphics Components

CSE 70 Final Exam Fall 2009

2. (True/False) All methods in an interface must be declared public.

Solution register itself

Wednesday, November 16, 11

Java Programming Lecture 7

C15: JavaFX: Styling, FXML, and MVC

Event-based Programming

Grafica e non solo: Java FX

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Events Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Threads & Timers. CSE260, Computer Science B: Honors Stony Brook University

Transcription:

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

Event Driven GUIs A Graphical User Interface (GUI) presents a graphical view of an application to users To build an event-driven GUI application, you must: Have a well-tested model that is independent of the view Make graphical components visible to the user Ensure the correct things happen for each event user clicks a button, or moves the mouse, or presses the enter key,... Let's first consider some of Java's GUI components: Pane, Button, Label, TextField 2

Graphical Components in JavaFX: JavaFX has many graphical components Stage: window with title, border, menu, buttons BorderPane: where we can add Buttons, Labels, (inside the Scene) Button: A component that can "clicked" Label: A display area for a small amount of text TextField: Allows editing of a single line of text 3

Get the app to show itself // Show an empty stage with no components in it public class FirstApp extends Application { } public static void main(string[] args) { launch(args); } The main entry point for all JavaFX apps The top level JavaFX container. @Override public void start(stage stage) throws Exception { stage.settitle("our First GUI"); BorderPane window = new BorderPane(); Scene scene = new Scene(window, 300, 90); // 300 pixels wide, 90 tall stage.setscene(scene); // Don't forget to show the running app: stage.show(); The container for all content One of many Pane Types

Add some components: So far we have an empty stage Let us add a Button, a Label, and a one line Editor (TextField) First construct three graphical components // Three different UI controls as instance variables private Button button = new Button("Nobody is listening to me"); private Label label = new Label("Button above, text field below"); private TextField textfield = new TextField("You can edit this text"); Need to add these objects to the BorderPane referenced by window 5

Components are nodes in a graph: Add three components to the BorderPane as Node objects window.settop(button); window.setcenter(label); window.setbottom(textfield); In addition to the 3 message above, we can setleft(node) setright(node) The Node objects are in a Pane object These nodes are children of the Pane The Pane is in a Scene object The Scene is in the Stage object 6

The 5 areas of BorderPane: By default, BorderPane objects have only five places where you can add components a 2nd add wipes out the 1st window.settop(new Button("Top")); window.setleft(new Button("Left")); window.setcenter(new Button("Center")); window.setright(new Button("Right")); window.setbottom(new Button("Bottom")); BTW: There is no padding or locating Nodes here The layout looks odd 7

There are many Panes with layout strategies: Pane Class BorderPane HBox, VBox GridPane TilePane FlowPane AnchorPane StackPane Strategy Areas for top, bottom, left, right, center Lines up children horizontally or vertically Layout children in a table like grid Layout children in a grid, all the same size Layout children left to right, top to bottom Children are positioned in relative position to the Pane's boundary Wraps children inside others, used to decorate such as putting a button over a a colored rectangle 8

AnchorPane: You can change the layout strategy with a different class of Pane With AnchorPane, we can position children specific number of pixels down from top of Pane specific number of pixels from the right of the Pane AnchorPane window = new AnchorPane(); AnchorPane.setTopAnchor(button, 5.0); AnchorPane.setRightAnchor(button, 60.0); AnchorPane.setTopAnchor(label, 35.0); AnchorPane.setRightAnchor(label, 60.0); textfield.setprefwidth(280); AnchorPane.setRightAnchor(textField, 10.0); AnchorPane.setTopAnchor(textField, 55.0); BorderPane AnchorPane 9

Event-Driven programming: 10

Event-Driven programming: 11

Event-Driven programming? A style of coding where a program's overall flow of execution is dictated by events The program loads The program waits for the user to generate input Each event causes some particular code to respond Need an event handler The overall flow of what code is determined by the user generating a series of events 12

What is Event-Driven programming? Contrast with application- or algorithm-driven control where program expects input data in a pre-determined order and timing Event-driven is a different programming paradigm Procedural (C) Object-Oriented (Java, Python) Event-driven (Java, Javascript) Declarative (SQL in 337 and 460) Functional (ML in 372) Logic (Prolog in 372) 13

There are many kinds of Events Different events that can occur in an event-driven program with a GUI Mouse move/drag/click, mouse button press/release Keyboard: key press/release Touchscreen finger tap/drag Joystick, drawing tablet, other device inputs Window resize/minimize/restore/close Network activity or file I/O (start, done, error) Timer interrupt Move a scroll bar Chose a menu selection Media finishes 14

Java s Event Model Java and the operating system work together to detect user interaction Button objects are notified when clicked Send a handle(actionevent) message to registered ActionEvent handlers TextField objects are notified when the user presses Enter A handle(actionevent) message is sent to registered event handlers When the mouse is clicked, the node under the curser is notified Send a handle(mouseevent) message to registered Mouse event handlers When a key is pressed Send a handle(keyevent) message to registered KeyEvent handlers 15

Example: ActionEvent The button and textfield do not yet perform any action Let s make something happen when The button is clicked The user presses enters into the textfield 16

How to Handle Events Add a private inner class that will handle the event that the component generates This class must implement an interface to guarantee that it has the expected method such as public void handle(actionevent ae) Register the event handler so the component can later send the correct message to that event handler Events occur anytime in the future--the event handler is waiting for user generated events such as clicking button Send this message to the GUI component: button.setonaction(handler) 17

Inner Classes: An inner class is a class defined within another class. Inner class methods can access the data from the scope in which they are defined. Inner classes can be hidden from other classes in the same package. 18

Event 1: Add Event to Handle a button press Must add a class that implements EventHandler<ActionEvent>. } EventHandler<ActionEvent> handler = new ButtonHandler(); button.setonaction(handler); stage.show(); private class ButtonHandler implements EventHandler<ActionEvent> { private int timesclicked; public ButtonHandler() { timesclicked = 0; } } @Override public void handle(actionevent arg0) { button.settext("i now have a handler"); timesclicked++; System.out.println("The button was clicked " + timesclicked + " times"); button.settext("i now have a handler"); } 19

Run this program 20

Event 2: Handle TextField Event: Must add another class that implements EventHandler<ActionEvent>. EventHandler<ActionEvent> handler2 = new TextFieldHandler(); textfield.setonaction(handler2); stage.show(); } private class TextFieldHandler implements EventHandler<ActionEvent> { private int enterpressed = 1; } @Override public void handle(actionevent arg0) { enterpressed++; String text = textfield.gettext(); if (enterpressed % 2 == 0) textfield.settext(text.touppercase()); else textfield.settext(text.tolowercase()); } 21

Run this Program: After pressing Enter key 22