23
Introduction to JavaFX Tecniche di Programmazione – A.A. 2012/2013

Introduction to JavaFX

Embed Size (px)

DESCRIPTION

Introduction to the JavaFX 2.0 framework. Topics: About and History Basic concepts Minimal JavaFX Application Teaching material for the course of "Tecniche di Programmazione" at Politecnico di Torino in year 2012/2013. More information: http://bit.ly/tecn-progr

Citation preview

Page 1: Introduction to JavaFX

Introduction to JavaFX

Tecniche di Programmazione – A.A. 2012/2013

Page 2: Introduction to JavaFX

Summary

A.A. 2012/2013 Tecniche di programmazione 2

1. About and History

2. Basic concepts

3. Minimal JavaFX Application

4. Resources

Page 3: Introduction to JavaFX

About and History

Introduction to JavaFX

Page 4: Introduction to JavaFX

GUI in Java

A.A. 2012/2013 Tecniche di programmazione 4

Graphic framework available in Java

Swing

Extremely powerful, many extensions available

Complex to master, requires low-level handling

Hard to create visually pleasing applications

Alternatives available

Most notable: SWT (Eclipse)

Still cumbersome to master

On a different Universe, web-based user interfaces

became nicer and faster to create

Page 5: Introduction to JavaFX

JavaFX 1.0 – forget it

A.A. 2012/2013 Tecniche di programmazione 5

JavaFX 1 and JavaFX 2 are completely different

Version 1 relied on a “scripting language” to describe

scenes, with ‘hooks’ to activate Java code

JavaFX 1.x is now deprecated

Page 6: Introduction to JavaFX

JavaFX 2.x

A.A. 2012/2013 Tecniche di programmazione 6

Redesigned from scratch

The JavaFX 2.x framework is entirely written in Java

For visual layout, an XML file may also be used (called

FXML)

Graphic appearance borrows from web-standard CSS

style sheets

UI programming is based on easy to handle events and

bindings

Oracle plans to deprecate Swing in favor of JavaFX 2

Page 7: Introduction to JavaFX

Getting and running JavaFX

A.A. 2012/2013 Tecniche di programmazione 7

JavaFX is already included in Oracle JDK 7

Not in JDK 6.x

Not in OpenJDK (beware, Linux users!)

Recommended:

JavaFX Scene Builder

Eclipse: e(fx)clipse plugin, available in the Eclipse Marketplace

Download links are in the course webpage

Page 8: Introduction to JavaFX

Basic concepts

Introduction to JavaFX

Page 9: Introduction to JavaFX

Key concepts in JavaFX

A.A. 2012/2013 Tecniche di programmazione 9

Stage: where the application will be displayed (e.g., a

Windows’ window)

Scene: one container of Nodes that compose one “page”

of your application

Node: an element in the Scene, with a visual appearance

and an interactive behavior. Nodes may be hierarchically

nested

My best friend is the JavaFX JavaDoc API

http://docs.oracle.com/javafx/2/api/index.html

Page 10: Introduction to JavaFX

Some Nodes (UI Form controls)

A.A. 2012/2013 Tecniche di programmazione 10

Page 11: Introduction to JavaFX

Some ‘Parent’ Nodes (Layout panes)

A.A. 2012/2013 Tecniche di programmazione 11

BorderPane (5-areas)

Hbox, Vbox (linear sequence)

StackPane (overlay all children)

GridPane (row x columns)

FlowPane (flowing boxes, wrap around)

TilePane (flowpane with equally sized boxes)

AnchorPane (magnetically attach nodes at corners or

sides)

Page 12: Introduction to JavaFX

Some Nodes (Charts)

A.A. 2012/2013 Tecniche di programmazione 12

Page 13: Introduction to JavaFX

Nodes family

A.A. 2012/2013 Tecniche di programmazione 13

javafx.scene.Node

Parent

Control

ChoiceBox

ComboBoxBase

ColorPicker

ComboBox

Labeled

ButtonBase

Button

CheckBox

MenuButton

ToggleButton

Cell

Label

TitledPane

ListView

MenuBar

Separator

Slider

TabPane

TextInputControl

TextArea

TextField

ToolBar

TreeView

Group

Region

Axis

Chart

Pane

AnchorPane

BorderPane

FlowPane

GridPane

HBox

StackPane

TilePane

VBox

WebView

Shape

Arc

Circle

Line

Polygon

Polyline

Rectangle

Text

Canvas

Imageview

Focus on

Panes

and

Controls

Page 14: Introduction to JavaFX

And more coming…

A.A. 2012/2013 Tecniche di programmazione 14

Page 15: Introduction to JavaFX

Empty JavaFX window

A.A. 2012/2013 Tecniche di programmazione 15

public class Main extends Application { @Override public void start(Stage stage) { Group root = new Group(); // the root is Group or Pane Scene scene = new Scene(root, 500, 500, Color.BLACK); stage.setTitle("JavaFX Demo"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }

Page 16: Introduction to JavaFX

Adding some shape

A.A. 2012/2013 Tecniche di programmazione 16

public class Main extends Application { @Override public void start(Stage stage) { Group root = new Group(); Scene scene = new Scene(root, 500, 500, Color.BLACK); stage.setTitle("JavaFX Demo"); Rectangle r = new Rectangle(25,25,250,250); r.setFill(Color.BLUE); root.getChildren().add(r); stage.setScene(scene); stage.show(); } }

Page 17: Introduction to JavaFX

How to add scene content

A.A. 2012/2013 Tecniche di programmazione 17

In Java code

By creating and adding new Node subclasses

Standard way, in Java (boring and error-prone)

By using node Builder classes

Programming pattern, later on…

In FXML

By writing XML directly

By using the Scene Editor

And loading the FXML into the application

Page 18: Introduction to JavaFX

JavaFX Scene Builder

A.A. 2012/2013 Tecniche di programmazione 18

Page 19: Introduction to JavaFX

FXML fragment

A.A. 2012/2013 Tecniche di programmazione 19

. . . <HBox id="HBox" alignment="CENTER" spacing="15.0" AnchorPane.rightAnchor="23.0" AnchorPane.topAnchor="22.0"> <children> <Button id="button1" fx:id="newIssue" onAction="#newIssueFired“ text="New" /> <Button id="button1" fx:id="saveIssue" onAction="#saveIssueFired“ text="Save" /> <Button id="button1" fx:id="deleteIssue" onAction="#deleteIssueFired" text="Delete" /> </children> </HBox> <ImageView id="IssueTrackingLite" layoutX="14.0" layoutY="20.0"> <image> <Image url="@IssueTrackingLite.png" preserveRatio="true" smooth="true" /> </image> </ImageView> . . .

Page 20: Introduction to JavaFX

Building a scene from FXML

A.A. 2012/2013 Tecniche di programmazione 20

public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load( getClass().getResource("circle.fxml")); stage.setTitle("Circle Demo"); stage.setScene(new Scene(root, 500, 150)); stage.show(); }

Page 21: Introduction to JavaFX

Resources

Introduction to JavaFX

Page 23: Introduction to JavaFX

Licenza d’uso

A.A. 2012/2013 Tecniche di programmazione 23

Queste diapositive sono distribuite con licenza Creative Commons “Attribuzione - Non commerciale - Condividi allo stesso modo (CC BY-NC-SA)”

Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,

rappresentare, eseguire e recitare quest'opera

di modificare quest'opera

Alle seguenti condizioni: Attribuzione — Devi attribuire la paternità dell'opera agli autori

originali e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera.

Non commerciale — Non puoi usare quest'opera per fini commerciali.

Condividi allo stesso modo — Se alteri o trasformi quest'opera, o se la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa.

http://creativecommons.org/licenses/by-nc-sa/3.0/