what's new for javafx in jdk8 - weaver

80
New JavaFX Features in Java 8 (and an intro to Lambdas) James Weaver Java Technology Ambassador Oracle Corporation @JavaFXpert [email protected]

Upload: codemotion

Post on 10-May-2015

1.526 views

Category:

Technology


1 download

DESCRIPTION

Le slide di James Weaver presentate a Codemotion Roma 2014

TRANSCRIPT

Page 1: What's new for JavaFX in JDK8 - Weaver

New JavaFX Features in Java 8(and an intro to Lambdas)

James WeaverJava Technology AmbassadorOracle Corporation

@[email protected]

Page 2: What's new for JavaFX in JDK8 - Weaver

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

ProgramAgenda

Introduction to JavaFX– Creating a user interface

– Understanding Scene Builder and FXML

New JavaFX Features in JDK 8– Some new Java features in JDK 8

– New JavaFX Features in JDK 8

Page 3: What's new for JavaFX in JDK8 - Weaver

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

About the presenterAuthor of several Java/JavaFX books

Page 4: What's new for JavaFX in JDK8 - Weaver

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

About the presenterCo-leader of IoT & JavaFX communities at java.net

javafxcommunity.com

iotcommunity.net

Page 5: What's new for JavaFX in JDK8 - Weaver

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

About the presenterFavorite TV shows are music competitions like “The Voice”

Page 6: What's new for JavaFX in JDK8 - Weaver

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

About the presenterI see that Italy has “The Voice” as well …

Page 7: What's new for JavaFX in JDK8 - Weaver

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

About the presenter… with a singing Nun. Very cool!

Page 8: What's new for JavaFX in JDK8 - Weaver

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

Java 8 Launch Event & Resource Videos

oracle.com/java8

Page 9: What's new for JavaFX in JDK8 - Weaver

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

IoT Developer Challenge

Show the world what you can do with Java + IoT for a chance to win a trip to JavaOne for you and two team members.

oracle.com/java8

Key Dates:Submissions begin March 3rd, 2014Submission deadline is May 30th, 2014Winners announced June 30th, 2014JavaOne 2014 from Sept. 28 to Oct. 2, 2014

Page 10: What's new for JavaFX in JDK8 - Weaver

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

■ An API included in Java SE 7/8 for UI development

■ The successor to Java Swing

■ Not the JavaFX Script language

10

JavaFX is:

Page 11: What's new for JavaFX in JDK8 - Weaver

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

Creating a User Interface

Page 12: What's new for JavaFX in JDK8 - Weaver

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

New JavaFX Application Dialog

Page 13: What's new for JavaFX in JDK8 - Weaver

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

Extend javafx.application.Application

Page 14: What's new for JavaFX in JDK8 - Weaver

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

Override the Application start() Method

Page 15: What's new for JavaFX in JDK8 - Weaver

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

Create the Circle

Page 16: What's new for JavaFX in JDK8 - Weaver

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

Create the Buttons and Handle Events

Page 17: What's new for JavaFX in JDK8 - Weaver

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

Layout the Buttons in an HBox

Page 18: What's new for JavaFX in JDK8 - Weaver

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

Create and Populate the Scene

Page 19: What's new for JavaFX in JDK8 - Weaver

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

Animate the Circle

Page 20: What's new for JavaFX in JDK8 - Weaver

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

Bind UI Properties to Animation Status

Page 21: What's new for JavaFX in JDK8 - Weaver

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

Setup and Show the Stage

Page 22: What's new for JavaFX in JDK8 - Weaver

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

Using Scene Builder and FXML

Page 23: What's new for JavaFX in JDK8 - Weaver

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

Scene Builder is a...

■ UI layout tool for JavaFX

■ Visual Editor for FXML

Page 24: What's new for JavaFX in JDK8 - Weaver

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

FXML is...

■ an XML-based language that expresses the UI

■ designed to be toolable

Page 25: What's new for JavaFX in JDK8 - Weaver

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

Download Scene Builderhttp://www.oracle.com/technetwork/java/javase/downloads

Page 26: What's new for JavaFX in JDK8 - Weaver

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

Scene Builder 2.0 Preview Available

Page 27: What's new for JavaFX in JDK8 - Weaver

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

Some New Java Features in JDK 8

■ Nashorn JavaScript Engine

■ Date & Time API (JSR-310)

■ Bulk data operations for collections

■ Lambda expressions and virtual extension methods

27

Page 28: What's new for JavaFX in JDK8 - Weaver

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

Compact ProfilesApproximate static footprint goals

Compact1 Profile

Compact2 Profile

Compact3 Profile

Full JRE 54Mb

30Mb

16Mb

11Mb

Page 29: What's new for JavaFX in JDK8 - Weaver

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

Java SE Embedded

Optimised binary for embedded use from devices to gateways Latest Java innovations in the smallest footprint Use of compact profiles

– Approximate JRE size 20.4Mb (ARM v7 VFS, Hard Float) Compact profile 1: 10.4Mb JavaFX: 10Mb

Production ready binary for popular platforms that run Linux– ARM v6/7 with Hard floating point

– Raspberry Pi is one of the reference platforms

Page 30: What's new for JavaFX in JDK8 - Weaver

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

A Lambda Expression (Closure) is…

…a function that that may have parameters, an expression or block of code, optionally returning a value.

30

e -> anim.playFromStart()

Page 31: What's new for JavaFX in JDK8 - Weaver

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

Event Handling w/Anon Inner Classes

Any interface that has exactly one abstract method is known as a functional interface, and may be replaced by a lambda expression.

Page 32: What's new for JavaFX in JDK8 - Weaver

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

Replace with Lambda Expression

The type of the lambda is inferred by the compiler as EventHandler<ActionEvent> because the onAction() method takes an object of type EventHandler<ActionEvent>. 

Because EventHandler has a single method handle() the lambda expression must be an implementation of the handle() method.

Page 33: What's new for JavaFX in JDK8 - Weaver

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

Simplify the Lambda Expression

The parameter in this lambda expression must be an ActionEvent, because that is the type specified by the handle() method of the EventHandler interface. 

We can therefore simplify this lambda expression because the parameter type is inferred.

Page 34: What's new for JavaFX in JDK8 - Weaver

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

Lose the Parentheses

When a lambda expression has a single parameter and its type is inferred, the parentheses are not required

Page 35: What's new for JavaFX in JDK8 - Weaver

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

Lose the Braces

Because the block of code in our lambda expression contains only one statement, we can lose the braces.

Page 36: What's new for JavaFX in JDK8 - Weaver

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

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

void createUI() { Button button = new Button("Guess What I'm Thinking"); button.setOnAction(e -> guess(e)); // lambda only calls a method...}

void guess(ActionEvent event) { Button button = (Button) event.getSource(); button.setText("You love Lambda!");}

If a lambda only calls a method …

Page 37: What's new for JavaFX in JDK8 - Weaver

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

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

… you can use a method reference

void createUI() { Button button = new Button("Guess What I'm Thinking"); button.setOnAction(this::guess); // use method reference ...}

void guess(ActionEvent event) { Button button = (Button) event.getSource(); button.setText("You love Lambda!");}

Page 38: What's new for JavaFX in JDK8 - Weaver

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

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

Four Kinds of Method References

1) Reference to a static method:ContainingClass::staticMethodName

2) Reference to an instance method of a particular object:ContainingObject::instanceMethodName

3) Reference to an instance method of an arbitrary object of a particular type: ContainingType::methodName

4) Reference to a constructor:ClassName::new

Page 39: What's new for JavaFX in JDK8 - Weaver

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

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

Another Method Reference Example

FileFilter x = f -> f.canRead(); // lambda only calls a method

FileFilter x = File::canRead(); // use method reference

Page 40: What's new for JavaFX in JDK8 - Weaver

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

JavaFXpert.com blog post with more detail

40

Page 41: What's new for JavaFX in JDK8 - Weaver

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

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

Nashorn is a replacement for Rhino. Its a bazilliontimes faster than Rhino. And it has some reallycool features, such as direct support for JavaFX.

Project Nashorn

Page 42: What's new for JavaFX in JDK8 - Weaver

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

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

Nashorn

var Rectangle = Java.type("javafx.scene.shape.Rectangle");var Color = Java.type("javafx.scene.paint.Color");var StackPane = Java.type("javafx.scene.layout.StackPane");var Scene = Java.type("javafx.scene.Scene");var RotateTransition = Java.type("javafx.animation.RotateTransition");var Duration = Java.type("javafx.util.Duration");

Page 43: What's new for JavaFX in JDK8 - Weaver

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

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

Nashornvar rect = new Rectangle();rect.width = 200;rect.height = 200;rect.stroke = Color.WHITE;rect.strokeWidth = 5;rect.fill = Color.ORANGE;rect.arcWidth = 20;rect.arcHeight = 20;var stack = new StackPane(rect);$STAGE.scene = new Scene(stack, 320, 240, Color.BLACK);

var tx = new RotateTransition(Duration.seconds(5), rect);tx.toAngle = 360;tx.autoReverse = true;tx.cycleCount = RotateTransition.INDEFINITE;tx.play();

Page 44: What's new for JavaFX in JDK8 - Weaver

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

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

Nashorn: Launching JavaFX

$JDK_HOME/bin/jjs -fx hello.js

Page 45: What's new for JavaFX in JDK8 - Weaver

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

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

Nashorn FX

Page 46: What's new for JavaFX in JDK8 - Weaver

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

New JavaFX Features in JDK 8■ New Modern Theme: Modena

■ Enhancements to Collections, Binding, Tasks and Services

■ Full screen improvements, new unified stage style, Swing integration improvements, rich text, and printing

■ DatePicker and TreeTableView

■ Public API for CSS structure

■ WebView Enhancements

■ JavaFX 3D, Multi-touch (in 7 but relevant to 3D)

■ Embedded Support

46

Page 47: What's new for JavaFX in JDK8 - Weaver

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

JavaFX 8New Theme

47

Caspian

vs

Modena

Page 48: What's new for JavaFX in JDK8 - Weaver

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

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

Modena

Page 49: What's new for JavaFX in JDK8 - Weaver

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

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

Modena

Page 50: What's new for JavaFX in JDK8 - Weaver

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

JavaFX 8

Customisable– Configurable key combinations to exit full screen mode

– Ability to prevent user exiting full screen mode

Improvements To Full Screen Mode

// Set the key combination that the user can use to exitstage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);

// Setup the click handler so we can escape from full screenRectangle r = new Rectangle(0, 0, 250, 250);r.setOnMouseClicked(e -> stage.setFullScreen(false));

// Set full screen againstage.setFullScreen(true);

Page 51: What's new for JavaFX in JDK8 - Weaver

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

JavaFX 8

New API for printing– Currently only supported on desktop

Any node can be printed

Printing Support

PrinterJob job = PrinterJob.createPrinterJob(printer);job.getJobSettings().setPageLayout(pageLayout);job.getJobSettings().setPrintQuality(PrintQuality.HIGH);job.getJobSettings().setPaperSource(PaperSource.MANUAL);job.getJobSettings().setCollation(Collation.COLLATED);

if (job.printPage(someRichText)) job.endJob();

Page 52: What's new for JavaFX in JDK8 - Weaver

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

JavaFX 8

DatePicker TreeTableView

New Controls

Page 53: What's new for JavaFX in JDK8 - Weaver

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

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

The JavaFX DatePicker is designed with the newCalendaring APIs in Java 8 using java.time

DatePicker

Page 54: What's new for JavaFX in JDK8 - Weaver

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

DatePicker

Page 55: What's new for JavaFX in JDK8 - Weaver

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

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

DatePicker

import java.time.LocalDate; // From new Date/Time API... LocalDate ld; DatePicker dp = new DatePicker(); dp.setOnAction(e -> { ld = dp.getValue(); System.out.println("Date selected " + ld); });

Page 56: What's new for JavaFX in JDK8 - Weaver

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

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

Date Picker

Page 57: What's new for JavaFX in JDK8 - Weaver

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

TreeTableViewUI control with combined TreeView and TableView controls functionality

57

Page 58: What's new for JavaFX in JDK8 - Weaver

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

JavaFX 8

Gestures– Swipe

– Scroll

– Rotate

– Zoom

Touch events and touch points

Touch Support

Page 59: What's new for JavaFX in JDK8 - Weaver

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

Touch Gestures

Page 60: What's new for JavaFX in JDK8 - Weaver

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

The Swipe Gesture

Commonly a finger drag in one direction A single event is produced for the gesture May be left, right, up, or down

Page 61: What's new for JavaFX in JDK8 - Weaver

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

Handling the Swipe Gesture (SwipeEvent)

Note: Lambda expressions from JDK 8 are used here to simplify event handling

Page 62: What's new for JavaFX in JDK8 - Weaver

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

Handling the Swipe Gesture (SwipeEvent)

Page 63: What's new for JavaFX in JDK8 - Weaver

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

The Scroll Gesture

User turns mouse wheel, drags finger on touch screen, etc.

Scroll events are continuously generated, containing x/y position-related info

Events are pixel-based or character/line-based

If inertia is supported, scroll events may be generated after user quits scrolling

Page 64: What's new for JavaFX in JDK8 - Weaver

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

Handling the Scroll Gesture (ScrollEvent)

Page 65: What's new for JavaFX in JDK8 - Weaver

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

The Rotate Gesture

User typically drags two fingers around each other

Rotate events are continuously generated, containing angle-related info

Page 66: What's new for JavaFX in JDK8 - Weaver

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

Handling the Rotate Gesture (RotateEvent)

Page 67: What's new for JavaFX in JDK8 - Weaver

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

The Zoom Gesture

User typically drags two fingers apart or closer together

Zoom events are continuously generated, containing zoom factor-related info

Page 68: What's new for JavaFX in JDK8 - Weaver

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

Handling the Zoom Gesture (ZoomEvent)

Page 69: What's new for JavaFX in JDK8 - Weaver

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

Handling Touch (TouchEvent/TouchPoint)

A TouchEvent contains information about a touch, including:

– Event type: Pressed, released, moved, or stationary

– Touch points: The TouchPoint instances that represent each of the points that were touched

Each TouchEvent has a unique ID to identify the events and touch points in a multi-touch action

Page 70: What's new for JavaFX in JDK8 - Weaver

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

Responding to Touch Events

Page 71: What's new for JavaFX in JDK8 - Weaver

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

JavaFX 8

Predefined shapes– Box

– Cylinder

– Sphere

User-defined shapes– TriangleMesh, MeshView

PhongMaterial Lighting Cameras

3D Support

Page 72: What's new for JavaFX in JDK8 - Weaver

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

Creating Primitive Shapes and Materials

Page 73: What's new for JavaFX in JDK8 - Weaver

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

Placing a Texture on a Sphere

73

Page 74: What's new for JavaFX in JDK8 - Weaver

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

Placing a Texture on a Sphere

74

Page 75: What's new for JavaFX in JDK8 - Weaver

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

3D Lights

■ Lights are nodes in the scene graph

■ PointLight

■ AmbientLight

■ Default light provided if no active lights

75

Page 76: What's new for JavaFX in JDK8 - Weaver

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

Lights, Camera, Action!

Page 77: What's new for JavaFX in JDK8 - Weaver

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

Example 3D multi-touch app: ZenGuitar3D

Page 78: What's new for JavaFX in JDK8 - Weaver

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

Questions?

Page 79: What's new for JavaFX in JDK8 - Weaver

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

Please note

The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Page 80: What's new for JavaFX in JDK8 - Weaver

New JavaFX Features in Java 8(and an intro to Lambdas)

James WeaverJava Technology AmbassadorOracle Corporation

@[email protected]