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

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

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

Building Graphical user interface using JavaFX

CS-202 Introduction to Object Oriented Programming

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

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

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

COMP6700/2140 Scene Graph, Layout and Styles

Graphical User Interfaces JavaFX GUI Basics. CSE114, Computer Science I Stony Brook University

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

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

Multimedia-Programmierung Übung 3

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

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

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

JavaFX. Getting Started with JavaFX Scene Builder Release 1.1 E

5 Drawing Stuff in 2D

Interaktionsprogrammering TDDD13 + TDDC73

JavaFX. JavaFX Overview Release E

JavaFX:Using Built-in Layout Panes

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

JAVAFX 101 [CON3826]

JavaFX Scene Builder

Week 5: Images & Graphics. Programming of Interactive Systems. JavaFX Images. images. Anastasia Bezerianos. Anastasia Bezerianos

Pro JavaFX 2. Weiqi Gao, Ph.D. Stephen Chin. Apress* James L. Weaver. Dean Iverson with Johan Vos, Ph.D.

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

Developing applications using JavaFX

C14: JavaFX: Overview and Programming User Interface

CO Java SE 7: Develop Rich Client Applications

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

Wednesday, November 16, 11

Beautiful User Interfaces with JavaFX

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

MARS AREA SCHOOL DISTRICT Curriculum TECHNOLOGY EDUCATION

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

Come organizzare gli ascoltatori/osservatori

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

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

CON Visualising GC with JavaFX Ben Evans James Gough

Eclipse + Html: A Journey

Introduction to Eclipse

COMP6700/2140 GUI and Event Driven Programming

Java Programming Layout

canoo Engineering AG


TLCPowerTalk.com. Communication for Management Professionals. QCon London 2009 (c) 12 March by Peter Pilgrim 1.

Computational Expression

CST141 JavaFX Basics Page 1

JavaFX Technology Building GUI Applications With JavaFX - Tutorial Overview

Educational Fusion. Implementing a Production Quality User Interface With JFC

C15: JavaFX: Styling, FXML, and MVC

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

Oracle JavaFX. JavaFX Scene Builder User Guide Release 1.1 E

C12: JavaFX Scene Graph, Events, and UI Components

Introduction p. 1 Java Features p. 2 Java Expansion p. 4 Getting, Setting Up, and Using Java p. 5 The Java Language p. 5 Java Swing Components p.

<Insert Picture Here> JavaFX Overview April 2010

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

Outline. Introduction to Java. What Is Java? History. Java 2 Platform. Java 2 Platform Standard Edition. Introduction Java 2 Platform

AIM. 10 September

C16a: Model-View-Controller and JavaFX Styling

Alexander Nyßen! itemis AG! Graphical Editing Framework Project Lead

light side dark side canoo

SHAPES & TRANSFORMS. Chapter 12 of Pro WPF : By Matthew MacDonald Assist Lect. Wadhah R. Baiee. College of IT Univ.

CHAPTER 1: A GENERAL INTRODUCTION TO PROGRAMMING 1

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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

Graphical User Interfaces

Distributed Multitiered Application

Graphics and Java 2D Introduction OBJECTIVES. One picture is worth ten thousand words.

Java.net - the Source for Java(tm) Technology Collaboration

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

Java: Graphical User Interfaces (GUI)

Design Document The Disease Outbreaks Team

Java FX. Properties and Bindings

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

Java FX 2.0. Dr. Stefan Schneider Oracle Deutschland Walldorf-Baden

Graphical User Interfaces

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

Multimedia-Programmierung Übung 4

Java Object Oriented Design. CSC207 Fall 2014

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

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!

Widget Toolkits CS MVC

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Alexander Nyßen! itemis AG! Graphical Editing Framework Project Lead. Image courtesy of Julie Lafrance / flickr

A Performance Tale. The evolution of binding in JavaFX. Robert Field Brian Goetz Sun Microsystems, Inc.

How to be a Good Bean

V-BOX Cloud Configuration

Final Assignment for CS-0401

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

Installation and Configuration Manual

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

Multimedia-Programmierung Übung 7

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

JAVA SYLLABUS FOR 6 MONTHS

LTBP INDUSTRIAL TRAINING INSTITUTE

Advanced Dreamweaver CS6

Transcription:

JavaFX Basics rights reserved. 1 Motivations JavaFX is a new framework for developing Java GUI programs. The JavaFX API is an excellent example of how the object-oriented principle is applied. This chapter serves two purposes. First, it presents the basics of JavaFX programming. Second, it uses JavaFX to demonstrate OOP. Specifically, this chapter introduces the framework of JavaFX and discusses JavaFX GUI components and their relationships. rights reserved. 2

JavaFX vs Swing and AWT Swing and AWT are replaced by the JavaFX platform for developing rich Internet applications. When Java was introduced, the GUI classes were bundled in a library known as the Abstract Windows Toolkit (AWT). AWT is fine for developing simple graphical user interfaces, but not for developing comprehensive GUI projects. In addition, AWT is prone to platform-specific bugs. The AWT user-interface components were replaced by a more robust, versatile, and flexible library known as Swing components. Swing components are painted directly on canvases using Java code. Swing componentsdependlessonthetargetplatformanduselessofthe native GUI resource. With the release of Java 8, Swing is replaced by a completely new GUI platform known as JavaFX. rights reserved. 3 Installing JavaFX& Scene Builder e(fx)clipse is an extension for your Eclipse IDE Extensions like this can be installed using the "Install New Software" wizard. Eclipsedoesnotknowaboutthelocationofthee(fx)clipsesoyou needtoaddtherepositoryasasoftwaresite. Theaddress is: http://download.eclipse.org/efxclipse/updates-released/1.2.0/site rights reserved. 4

Installing JavaFX& Scene Builder In the tree of installable features, check only the entry corresponding to your release: e(fx)clipse- IDE Also, make sure the "Contact all update sites during install to find required software" is checked. You may, however, install the FX time feature into your IDE but we strongly recommend composing your own target platform. When requested, restart Eclipse. rights reserved. 5 Installing JavaFX& Scene Builder The JavaFX Scene Builder is available as a Windows package (.msi) for the Windows platform, as a Debian package(.deb) or.tar.gzfileforthelinuxplatform,andasadiskimage(.dmg)for themacosxplatform. Download JavaFX Scene Builder from the Additional Resources section of the Java SE Downloads page at http://www.oracle.com/technetwork/java/javase/downloads/index.html By default, the JavaFX Scene Builder software is installed at C:\Program Files\Oracle\JavaFX Scene Builder 2.0 on a Windows platform. If you install JavaFX Scene Builder on a 64-bit Windows machine, the default installation location is C:\Program Files(x86)\Oracle\JavaFX Scene Builder 2.0. rights reserved. 6

Installing JavaFX& Scene Builder rights reserved. 7 Eclipse Configurations WeneedtotellEclipsetouseJDK8andalsowhereitwillfind the Scene Builder: 1. Open the Eclipse Preferences and navigate to Java Installed JREs. 2. Click Add..., select Standard VM and choose the installation Directory of yourjdk8. 3. RemovetheotherJREsorJDKssothattheJDK8becomesthedefault. rights reserved. 8

Eclipse Configurations 4. Navigate to Java Compiler. Set the Compiler compliance level to 1.8. rights reserved. 9 Eclipse Configurations 5. Navigate to the JavaFX preferences. Specify the path to your Scene Builder executable. rights reserved. 10

Basic Structure of JavaFX Application Override the start(stage) method Stage, Scene, and Nodes Stage Scene Button MyJavaFX MultipleStageDemo rights reserved. 11 Basic Structure of JavaFX It'slikeatheaterplay:TheStageisthemaincontainerwhichis usually a Window with a border and the typical minimize, maximize and close buttons. Inside the Stage you add a Scene whichcan,ofcourse,beswitchedoutbyanotherscene.insidethe Scene the actual JavaFX nodes like AnchorPane, TextBox, etc. are added. rights reserved. 12

Example JavaFX Types Classes o JavaFX Main Class Extends JavaFX Application Contains main() and launch() launch() will initialise and call start() which must be defined o FXML XML based language Describes UI appearance Controlled by Scene Builder UI Layouts o Rootpane Base forui scene Can havea menubar o AnchorPane Used to anchor components o SplitPane o TableView o GridPane o TilePane o Hbox Setsboxwrappertocontain horizontal components rights reserved. 13 JavaFX Classes o Stage Represents the main container and window o Scene Adds features to Stage, representing FXML layouts o FXMLLoader Handles parsing of FXML files into Java Class @FXML keyword links private Java variables to corresponding FXML components @FXML private TableView<person> persontable; o JavaFX Collections Data Structures that have Event Listeners Can handle changes made in GUI automatically ObservableList is example of array with Listener rights reserved. 14

What is FXML? FXML is an XML-based language that provides the structure for building a user interface separate from the application logic of your code. This separation of the presentation and application logic is attractive to web developers because they can assemble a user interface that leverages Java components without mastering thecodeforfetchingandfillinginthedata. FXML does not have a schema, but it does have a basic predefined structure. What you can express in FXML, and how itappliestoconstructingascenegraph,dependsontheapiof the objects you are constructing. Because FXML maps directly to Java, you can use the API documentation to understand what elements and attributes are allowed. In general, most JavaFX classes can be used as elements, and most Bean properties can be used as attributes. rights reserved. 15 FXML and Scene Builder Just as some developers prefer to work directly in the XML code, other developersprefertouseatooltoauthortheirxml.thesameistrue with FXML. Ifyouprefertouseatool,orifyouwanttocreateaquickprototypetoget feedback, then you might prefer to use JavaFX Scene Builder. SceneBuilderisadesigntoolthatgeneratestheFXMLsourcecodeasyou define the user interface for your application. Scene Builder can help you to quickly create a prototype for an interactive application that connects components to the application logic. Because Scene Builder uses XML as a serialization format, the produced FXMLcodeisveryclearandyoucanfurthereditFXMLfiles,generatedby SceneBuilder,inanytextorXMLeditor. rights reserved. 16

Scene Builder rights reserved. 17 FXML UI Components rights reserved. 18

CSS Styling CanstyleFXMLfeatureswithCSS Edit appearance using controls for colour, background, text and sizing SimilartousingCSSwithHTML rights reserved. 19 Advanced JavaFX Components o FileChooser Opens Directory Browser o TreeView Creates hierarchical list views for nest features o HTMLEditor Advanced text editor with HTML features o WebViewer Inbuilt web display Handles HTML5, CSS, JavaScript, DOM, SVG rights reserved. 20

Panes, UI Controls, and Shapes ButtonInPane rights reserved. 21 Display a Shape This example displays a circle in the center of the pane. (0, 0) X Axis Y Axis y Y Axis (x, y) Java Coordinate System Conventional Coordinate System (0, 0) X Axis ShowCircle rights reserved. 22

Binding Properties JavaFX introduces a new concept called binding property that enables a target objectto be bound to a source object. If the value in the source object changes, the target property is also changed automatically. The target object is simply called a binding objector a binding property. ShowCircleCentered rights reserved. 23 Binding Property: getter, setter, and property getter rights reserved. 24

Common Properties and Methods for Nodes style: set a JavaFX CSS style rotate: Rotate a node NodeStyleRotateDemo rights reserved. 25 The Color Class rights reserved. 26

The Font Class FontDemo rights reserved. 27 The Image Class rights reserved. 28

The ImageView Class ShowImage rights reserved. 29 Layout Panes JavaFX provides many types of panes for organizing nodes in a container. rights reserved. 30

FlowPane ShowFlowPane rights reserved. 31 GridPane ShowGridPane rights reserved. 32

BorderPane ShowBorderPane rights reserved. 33 HBox rights reserved. 34

VBox ShowHBoxVBox rights reserved. 35 Shapes JavaFX provides many shape classes for drawing texts, lines, circles, rectangles, ellipses, arcs, polygons, and polylines. rights reserved. 36

Text rights reserved. 37 Text Example ShowText rights reserved. 38

Line ShowLine rights reserved. 39 Rectangle rights reserved. 40

Rectangle Example ShowRectangle rights reserved. 41 Circle rights reserved. 42

Ellipse radiusx radiusy (centerx, centery) ShowEllipse rights reserved. 43 Arc rights reserved. 44

Arc Examples radiusy length startangle 0 degree radiusx (centerx, centery) 30 20 50 20 (a) Negative starting angle 30 and negative spanning angle 20 (b) Negative starting angle 50 and positive spanning angle 20 ShowArc rights reserved. 45 Polygon and Polyline ShowArc rights reserved. 46

Polygon +Polygon() javafx.scene.shape.polygon +Polygon(double... points) +getpoints(): ObservableList<Double> The getter and setter methods for property values and a getter for property itself are provided in the class, but omitted in the UML diagram for brevity. Creates an empty polygon. Creates a polygon with the given points. Returns a list of double values as x- and y-coordinates of the points. ShowPolygon rights reserved. 47 Case Study: The ClockPane Class This case study develops a class that displays a clock on a pane. javafx.scene.layout.panel -hour: int -minute: int -second: int ClockPane +ClockPane() +ClockPane(hour: int, minute: int, second: int) +setcurrenttime(): void +setwidth(width: double): void +setheighttime(height: double): void The getter and setter methods for these data fields are provided in the class, but omitted in the UML diagram for brevity. The hour in the clock. The minute in the clock. The second in the clock. Constructs a default clock for the current time. Constructs a clock with the specified time. Sets hour, minute, and second for current time. Sets clock pane s width and repaint the clock, Sets clock pane s height and repaint the clock, ClockPane rights reserved. 48

Use the ClockPane Class DisplayClock rights reserved. 49