Top Banner
JavaFX because you're worth it Thierry Wasylczenko @twasyl
61

JavaFX, because you're worth it

Dec 05, 2014

Download

Technology

twasyl

My slides presented during SoftShake 2013 (October, 24th)
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JavaFX, because you're worth it

JavaFXbecause you're worth it

Thierry Wasylczenko@twasyl

Page 2: JavaFX, because you're worth it

me.getInfo();

Software & quality engineer @ GE HealthcareJFXtras contributorOpenJFX in progressFormer Java Full Professor & teacher @ SUPINFOSpeaker

Page 3: JavaFX, because you're worth it

We all know this...

Page 4: JavaFX, because you're worth it

But also this...

private class MyFrame extends JFrame {

private void initComponents() { jLabel1 = new JLabel(); jTextField1 = new JTextField(); jLabel2 = new JLabel(); jTextField2 = new JTextField(); jLabel3 = new JLabel(); jRadioButton1 = new JRadioButton(); jRadioButton2 = new JRadioButton(); jButton1 = new JButton(); jScrollPane1 = new JScrollPane(); jTable1 = new JTable(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("Person name:"); jLabel2.setText("Person first name:"); jLabel3.setText("Sex:"); jRadioButton1.setText("Male"); jRadioButton1.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { // TODO } }); // TO BE CONTINUED }}

Page 5: JavaFX, because you're worth it

To get this...

Page 6: JavaFX, because you're worth it

The big button syndrome

Page 7: JavaFX, because you're worth it

Desktop apps in Java

Abstract Window Toolkit (AWT)SwingEvent Dispatch Thread (EDT)ComplexL&F

Old lookingNimbusSynthetica, JGoodies Looks, SWT Ribbon, Quaqua,Substance, ...

Page 8: JavaFX, because you're worth it

Some tools "helped" us ...

Visual EditorEclipse pluginHow many JARs do I need to start creating aUI??!!

NetBeans

Page 9: JavaFX, because you're worth it

... or not !

So much junk code !!Not even editable

Except outside the IDE

Page 10: JavaFX, because you're worth it

A hope shined ...

JavaOne 2007JavaFX is announcedRich Internet Applications (RIA)"Seems" promising

Page 11: JavaFX, because you're worth it

... but ...

Java + Flash + Flex

=

JavaFX

=

???

Page 12: JavaFX, because you're worth it

I am a Java developer !!

Scripting languageSimilar to Flash scripting languageUnusable Java API

JARs? What's that?Hey, I'm a Java developer !!

Page 13: JavaFX, because you're worth it

Look through the Windows

Windows Presentation Foundation(WPF)Works above DirectXIntroduced with .NET 3.0XAMLHey, I'm still a Java developer !

eface

Page 14: JavaFX, because you're worth it

And now ...

Page 15: JavaFX, because you're worth it

JavaFX 2

Page 16: JavaFX, because you're worth it
Page 17: JavaFX, because you're worth it

JavaFX architecture

Page 18: JavaFX, because you're worth it

Base classes

ApplicationThe entry point of your appApplication.launch()Override start()

SceneLike the content pane in SwingHosts a root element

Stage = JFrameContains a scenestage.show();

Page 19: JavaFX, because you're worth it

Base classes

public class TweetWallFX extends Application {

public static void main(String[] args) {

Application.launch(TweetWallFX.class, args);

}

@Override public void start(Stage stage) throws Exception {

// Do your stuff here

Scene scene = new Scene( ... ); stage.setScene(scene); stage.show(); }

}

Page 20: JavaFX, because you're worth it

FXML

MVCMarkup-based languageCSS stylingJavaScript capabilitiesComponent binding

Controller conceptURL locatorI18N

Page 21: JavaFX, because you're worth it

FXML

<?xml version="1.0" encoding="UTF-8"?><?import java.lang.*?><?import javafx.scene.*?><?import javafx.scene.effect.*?><?import javafx.scene.image.*?><?import javafx.scene.control.*?><?import javafx.scene.layout.*?>

<AnchorPane xmlns:fx="http://javafx.com/fxml" id="mainPanel" fx:id="anchorPane" prefHeightstyle="-fx-background-color: linear-gradient(#69B4E4 0%, #0070B9 100%);" > <children> <ImageView id="twitterSticker" fitWidth="128" fitHeight="128" layoutX="30" <image> <Image url="@/com/twasyl/tweetwallfx/resources/images/twitterSticker.png" </image> </ImageView> <Label text="TweetWallFX" prefWidth="1024" style="-fx-font-size: 100pt; -fx-text-fill: white; -fx-font-weight: bold;" <Button id="button" text="Search" onAction="#startTweetWall" style="-fx-font-size: 20pt;" </children></AnchorPane>

Page 22: JavaFX, because you're worth it

JavaFX CSS

Properties prefixed with -fx-fx-text-fill-fx-background-color...

linear-gradientradial-gradientrgb / rgbaEffects

dropshadow &innershadow

Page 23: JavaFX, because you're worth it

JavaFX CSS

Inline CSS

<Label text="TweetWallFX" prefWidth="1024" style="-fx-font-size: 100pt; -fx-text-fill: white; -fx-font-weight: bold;"

Page 24: JavaFX, because you're worth it

JavaFX CSS

Stylesheets and CSS classes

<AnchorPane ...> <stylesheets> <URL value="@/com/twasyl/Default.css" /> </stylesheets> <children> <Label styleClass="cool-class" /> <Label> <styleClass> <String fx:value="very-cool-class" /> <String fx:value="amazing-class" /> </styleClass> </Label> <children></AnchorPane>

Page 25: JavaFX, because you're worth it

JavaFX CSS

Java code

myButton.getStyleClass().addAll("cool-class", "very-cool-class");myButton.setStyle("-fx-background-color: white");

Page 26: JavaFX, because you're worth it

Controller

Similar to master pages in ASPSimilar to a Managed BeanUsed to

manage eventsupdate the view dynamically...

Referenced by fx:controller in the FXMLInitializable

Used to initialize the controller after the rootelement

Page 27: JavaFX, because you're worth it

Controller

public class TweetWallFXController implements Initializable {

// ...

@Override public void initialize(URL arg0, ResourceBundle arg1) {

// ...

}}

Page 28: JavaFX, because you're worth it

@FXML

Component bindingLike JSFEach attribute must be strictly named as the fx:idin the FXML

Controller Method Event handleronAction, onClosed, ... attributesReferenced with # in FXML

Page 29: JavaFX, because you're worth it

@FXML

The controller

// ...

@FXML private Pane anchorPane;

@FXML public void startTweetWall(ActionEvent e) {// ...}

// ...

Page 30: JavaFX, because you're worth it

@FXML

The FXML

<AnchorPane ... fx:id="anchorPane" ... fx:controller="com.twasyl.tweetwallfx.controllers.TweetWallFXController" <children> <Button id="button" onAction="#startTweetWall" ... />

</children></AnchorPane>

Page 31: JavaFX, because you're worth it

FXMLLoader

Load a FXML fileResourceBundle can be defined

Get the controller

Page 32: JavaFX, because you're worth it

FXMLLoader

URL fxmlURL = getClass().getResource("/my/package/myFile.fxml");FXMLLoader fxml = new FXMLLoader(fxmlURL);

// Get the root element of the fileParent root = (Parent) fxml.load();

// Get the controller associated to the FXMLMyController mc = (MyController) fxml.getController();

Page 33: JavaFX, because you're worth it

Properties

Expand & improve the JavaBeans concept"Observe" a value

Event deliveryListeners

Conventionprivate property fieldspublic final getter / setter for the valuepublic getter for the property

Page 34: JavaFX, because you're worth it

Properties

public class Foo {

private DoubleProperty litersOfCoffee = new SimpleDoubleProperty();

public final double getLitersOfCoffee() {

return this.litersOfCoffee.get();

}

public final void setLitersOfCoffee(double value) {

this.litersOfCoffee.set(value);

}

public DoubleProperty litersOfCoffeeProperty() {

return this.litersOfCoffee;

}

}

Page 35: JavaFX, because you're worth it

Bindings

Observes dependenciesProperties

Updates itself according changesHigh-Level APILow-Level APIKind of Observer / Observable patternExample:

Could be used to synchronize the UI and thebusinessRefresh a chart data

Page 36: JavaFX, because you're worth it

Bindings

IntegerProperty num1 = new SimpleIntegerProperty(10);

IntegerProperty num2 = new SimpleIntegerProperty(20);

IntegerProperty num3 = new SimpleIntegerProperty(30);

IntegerProperty num4 = new SimpleIntegerProperty(40);

NumberBinding operation = Bindings.add(num1.multiply(num4), num2.divide(num3));

System.out.println("How much? " + operation.getValue());

num1.setValue(100);

System.out.println("And now? " + operation.getValue());

Page 37: JavaFX, because you're worth it

Bindings

How much? 400.67

And now? 4000.67

Page 38: JavaFX, because you're worth it
Page 39: JavaFX, because you're worth it
Page 40: JavaFX, because you're worth it

FXCollections

Extension of CollectionsObservableInterfaces

ObservableList, ObservableMapListeners

ListChangeListener, MapChangeListenerFXCollections

Utility class

Page 41: JavaFX, because you're worth it

FXCollections

List<String> stringLst = new ArrayList<String>();

// Create an observable listObservableList<String> stringObsLst1 = FXCollections.observableList(stringLst);

ObservableList<String> stringObsLst2 = FXCollections.observableList();

stringObsLst1.addListener(new ListChangeListener<String>() {

@Override public void onChanged(Change<? extends String> event) {

while(event.next()) {

if(event.wasAdded()) {

// ...

} else if(event.wasRemoved()) {

// ... } } }}

Page 42: JavaFX, because you're worth it

Chart API

Page 43: JavaFX, because you're worth it
Page 44: JavaFX, because you're worth it

Chart API

Page 45: JavaFX, because you're worth it
Page 46: JavaFX, because you're worth it

Animation

Page 47: JavaFX, because you're worth it
Page 48: JavaFX, because you're worth it

Animation

KeyValueRepresent a "fixed" position of a Node

KeyFrameA durationSome KeyValues

AnimationCould contain some KeyFrames (Timeline)Play

Page 49: JavaFX, because you're worth it

Animation

Node node = ... ;

KeyValue kv = new KeyValue(node.layoutXProperty(), 100);

KeyFrame kf = new KeyFrame(new Duration(5000), kv);

Timeline timeline = new Timeline(kf);

timeline.setCycleCount(Animation.INDEFINITE);

timeline.setOnFinished( ... );

timeline.play();

Page 50: JavaFX, because you're worth it

Effects

Lot of effects availableBlur, blend, reflection, shadows, ...

Input propertyChain of effects

Page 51: JavaFX, because you're worth it

Media

Long way from JMFAudio

MP3, AIFF, WAV, AAC,m4a

VideoFLV, MPEG-4(H.264/AVC)

MetadataListeners

Page 52: JavaFX, because you're worth it

Media

File mySong = new File("/mySong.mp3");

Media myMedia = new Media(mySong.toURI().toString());

MediaPlayer player = new MediaPlayer(myMedia);

player.setOnEndOfMedia( new Runnable() { ... });

player.volumeProperty().addListener( ... );

player.play();

// ...

if(player.statusProperty().get() == Status.PLAYING) { // ...}

Page 53: JavaFX, because you're worth it

SceneBuilder

UI builder toolUseful for RAD

FXML generationCSS supportPreviewer

Page 54: JavaFX, because you're worth it

SceneBuilder

Page 55: JavaFX, because you're worth it

Scenic View

Understand current state of your applicationManipulation of the scenegraphManipulation of properties

Page 56: JavaFX, because you're worth it

Scenic View

Page 57: JavaFX, because you're worth it
Page 58: JavaFX, because you're worth it

Tools / API

JFXtrasFX Experience tool

Theminge(fx)clipseGroovyFXScalaFX...

Page 59: JavaFX, because you're worth it

Useful resources

http://thierrywasyl.wordpress.com

Page 60: JavaFX, because you're worth it

http://thierrywasyl.wordpress.comhttp://docs.oracle.com/javafx/index.htmlhttps://forums.oracle.com/forums/forum.jspa?forumID=1385http://fxexperience.comhttp://jfxtras.org

Page 61: JavaFX, because you're worth it

Thank you