what's new for javafx in jdk8 - weaver
DESCRIPTION
Le slide di James Weaver presentate a Codemotion Roma 2014TRANSCRIPT
New JavaFX Features in Java 8(and an intro to Lambdas)
James WeaverJava Technology AmbassadorOracle Corporation
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.3
About the presenterAuthor of several Java/JavaFX books
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.5
About the presenterFavorite TV shows are music competitions like “The Voice”
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.6
About the presenterI see that Italy has “The Voice” as well …
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.7
About the presenter… with a singing Nun. Very cool!
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.8
Java 8 Launch Event & Resource Videos
oracle.com/java8
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
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:
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.11
Creating a User Interface
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.12
New JavaFX Application Dialog
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.13
Extend javafx.application.Application
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.14
Override the Application start() Method
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.15
Create the Circle
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.16
Create the Buttons and Handle Events
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.17
Layout the Buttons in an HBox
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.18
Create and Populate the Scene
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.19
Animate the Circle
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.20
Bind UI Properties to Animation Status
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.21
Setup and Show the Stage
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.22
Using Scene Builder and FXML
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.23
Scene Builder is a...
■ UI layout tool for JavaFX
■ Visual Editor for FXML
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.25
Download Scene Builderhttp://www.oracle.com/technetwork/java/javase/downloads
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.26
Scene Builder 2.0 Preview Available
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
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
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
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()
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.
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.
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.
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
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.
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 …
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!");}
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
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.40
JavaFXpert.com blog post with more detail
40
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
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");
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();
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.4545
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Nashorn FX
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.47
JavaFX 8New Theme
47
Caspian
vs
Modena
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.4848
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Modena
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.4949
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Modena
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);
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();
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.52
JavaFX 8
DatePicker TreeTableView
New Controls
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.54 54
DatePicker
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); });
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.5656
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Date Picker
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.57
TreeTableViewUI control with combined TreeView and TableView controls functionality
57
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.59
Touch Gestures
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
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.62
Handling the Swipe Gesture (SwipeEvent)
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.64
Handling the Scroll Gesture (ScrollEvent)
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.66
Handling the Rotate Gesture (RotateEvent)
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.68
Handling the Zoom Gesture (ZoomEvent)
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.70
Responding to Touch Events
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.72 72
Creating Primitive Shapes and Materials
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.73
Placing a Texture on a Sphere
73
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.74
Placing a Texture on a Sphere
74
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.76 76
Lights, Camera, Action!
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.77
Example 3D multi-touch app: ZenGuitar3D
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.78
Questions?
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.
New JavaFX Features in Java 8(and an intro to Lambdas)
James WeaverJava Technology AmbassadorOracle Corporation