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

[English version] JavaFX and Web Integration

Embed Size (px)

DESCRIPTION

One of the key features of JavaFX 2.0 is having full-fledged embedded browser. This enables JavaFX apps to embed web contents, and also to work together with HTML5 apps. This session focuses on JavaFX's web component, WebEngine, and show how JavaFX can work together with web technologies (HTML/CSS/JavaScript) by using demos and sample codes. (Session JS1-13 / Apr 4th, JavaOne Tokyo 2012)

Citation preview

Page 1: [English version] JavaFX and Web Integration

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

Page 2: [English version] JavaFX and Web Integration

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

JavaFX and Web Integration

Kazuchika Sekiya

Java Embedded Global Business Unit, Oracle Japan English version

Page 3: [English version] JavaFX and Web Integration

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

The following 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 4: [English version] JavaFX and Web Integration

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

Servers Desktop Embedded TV Mobile Card

Java Card

Java EE Java TV

BD-J

JavaFX MSA

Java Language

Java SE Java ME

Key APIs

Platform

Language

Java Platform

Page 5: [English version] JavaFX and Web Integration

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

What is JavaFX? Next Generation Java Client Solution

Advanced Graphics

Web Media

Audio/Video HTML5/CSS3/JavaScript

Animations Effects 3D

Page 6: [English version] JavaFX and Web Integration

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

JavaFX 2.0 Architecture

Page 7: [English version] JavaFX and Web Integration

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

Agenda

• Overview

• API: WebEngine and WebView

• Advanced Features

Page 8: [English version] JavaFX and Web Integration

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

JavaFX Web Component

• Full-fledged built-in browser

– HTML4, partial HTML5 support

– JavaScript engine

– LiveConnect

– DOM access

– SVG support

Page 9: [English version] JavaFX and Web Integration

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

Use Cases

• Showing Web content

– Remote content, locally generated content

• Embedding Web applications

– Using Java to control Web apps

– Enhance Web apps by a variety of Java libraries

Page 10: [English version] JavaFX and Web Integration

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

Implementation Notes

• Based on WebKit browser engine

– Networking is implemented using java.net

– Rendering is implemented using JavaFX (Prism)

• Provided as a scene graph node

– Effects, transforms and transitions can be applied

Page 11: [English version] JavaFX and Web Integration

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

Performance

• GUIMark2 HTML5 test http://www.craftymind.com/guimark2

Windows

Vector Test Bitmap Test Text Test

Chrome

17.0 16.4 22.1 20.6

Firefox

10.0.2 12.2 9.8 28.4

JavaFX

2.0.3 15.1 30.2 5.8

Windows 7 Professional / Intel Core 2 Duo 2.53GHz / RAM 4.0GB

Page 12: [English version] JavaFX and Web Integration

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

Performance

• GUIMark2 HTML5 test http://www.craftymind.com/guimark2

Mac

Vector Test Bitmap Test Text Test

Chrome

18.0 16.9 56 18.9

Safari

5.1.5 3.5 14.5 23.8

JavaFX

2.1 beta b19 15.5 31.1 7.0

Mac OS X 10.6.8 / Intel Core 2 Duo 2.13GHz / RAM 4.0GB

Page 13: [English version] JavaFX and Web Integration

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

DEMO

Page 14: [English version] JavaFX and Web Integration

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

Agenda

• Overview

• API: WebEngine and WebView

• Advanced Features

Page 15: [English version] JavaFX and Web Integration

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

WebEngine and WebView

Page 16: [English version] JavaFX and Web Integration

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

WebEngine

• Non visual component

• Provides following functions:

– Loads Web pages

– Runs scripts on pages

– Manages DOM

• Can be used without WebView

package javafx.scene.web;

Page 17: [English version] JavaFX and Web Integration

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

WebView

• Is a scene graph Node

– Used to display Web pages

• Has an associated WebEngine

– Created at construction time

– Cannot be changed

– WebView.getEngine()

package javafx.scene.web;

Page 18: [English version] JavaFX and Web Integration

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

Key Methods of WebEngine

Method Description

void load(String url) Loads a Web page

void loadContent(String content) Loads the given HTML content

void reload() Reloads the current page

Worker getLoadWorker() Returns a Worker to track loading

Object executeScript(String script) Executes a script

Document getDocument() Returns the DOM for current page

Page 19: [English version] JavaFX and Web Integration

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

Tracking of Web Page Loading

• Loading is asynchronous

• Use WebEngine.getLoadWorker() to track loading

– state

• READY -> SCHEDULED -> RUNNING ->

SUCCEEDED/FAILED/CANCELED

– progress (0…1)

– workDone / totalWork

– Can cancel() loading

package javafx.concurrent;

Page 20: [English version] JavaFX and Web Integration

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

Basic Usage of Web Component

import javafx.scene.web.WebView; import javafx.scene.web.WebEngine; WebView view = new WebView(); // add WebView to scene graph // get WebEngine from WebView WebEngine engine = view.getEngine(); // load a URL engine.load("http://javafx.com");

Page 21: [English version] JavaFX and Web Integration

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

DEMO

Page 22: [English version] JavaFX and Web Integration

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

Agenda

• Overview

• API: WebEngine and WebView

• Advanced Features

Page 23: [English version] JavaFX and Web Integration

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

DOM (Document Object Model) Access

• WebEngine.document property / getDocument() method

– Initialized at some point before page is loaded

– Read only

– But you can modify the Document structure!

Page 24: [English version] JavaFX and Web Integration

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

Example: Modifying DOM

Document = engine.getDocument(); Element para2 = doc.getElementById("para2"); Element newp = doc.createElement("p"); newp.appendChild(doc.createTextNode("new paragraph")); para2.getParentNode().insertBefore(newp, para2);

Page 25: [English version] JavaFX and Web Integration

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

DEMO

Page 26: [English version] JavaFX and Web Integration

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

contentEditable

• An attribute introduced in HTML5

• To make region editable, do one of:

// use Element interface of DOM inputLine.setAttribute("contenteditable", "true"); // or, casting to JSObject ((JSObject)inputLine).setMember("contentEditable", true)

Page 27: [English version] JavaFX and Web Integration

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

JavaScript Evaluation

• WebEngine.executeScript() method

– evaluate a JavaScript expression in the context of the current page

public String getPath() { return (String)webEngine.executeScript("location.pathname"); }

Page 28: [English version] JavaFX and Web Integration

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

Translating JavaScript value to Java

JavaScript Java

null null

undefined “undefined”

number java.lang.Number

(Integer or Double)

string java.lang.String

boolean java.lang.Boolean

object netscape.javascript.JSObject

Page 29: [English version] JavaFX and Web Integration

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

JSObject

• Java wrapper around JavaScript object

• Part of LiveConnect API

package netscape.javascript;

Key Method Description

Object getMember(String name) Retrieves a named member

void setMember(String name, Object value) Sets a named member

void removeMember(String name) Removes a named member

Object call(String method, Object[] args) Calls a JavaScript method

Page 30: [English version] JavaFX and Web Integration

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

Node implements JSObject

• DOM Node implementation also implements JSObject

– same object wraps both the native WebKit (C++) DOM, and

wraps JavaScripts DOM (which also wrap native DOM)

• You do need to cast:

Element inputLine = ...; // get an Element // use it as JSObject ((JSObject)inputLine).call("focus");

Page 31: [English version] JavaFX and Web Integration

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

Calling Java from JavaScript

1. Create an interface object (of any class)

2. Make it known to JavaScript by calling

JSObject.setMember()

⇒You can call public methods from JavaScript

Page 32: [English version] JavaFX and Web Integration

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

Calling Java from JavaScript

JSObject jsobj = (JSObject)webEngine.executeScript("window"); jsobj.setMember("java", new Bridge());

class Bridge { public void exit() { Platform.exit(); } }

<p>Click <a href="" onclick="java.exit();">here</a> to exit the application</p>

Page 33: [English version] JavaFX and Web Integration

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

Threading

• JavaFX, like Swing, is single-threaded

• WebKit is single-threaded as well

⇒Web Component must only be accessed from the

JavaFX application thread

– use javafx.application.Platform.runLater() method

Page 34: [English version] JavaFX and Web Integration

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

DEMO

Page 35: [English version] JavaFX and Web Integration

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

Control Google Map using Wii Remote

Wii Remote

Google Map

WiiRemoteJ

BlueCove

WebEngine/WebView

Bluetooth

JavaScript API call

Page 36: [English version] JavaFX and Web Integration

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

Integrate Twitter4J library and HTML5 App

HTML5 app

“Ball Pool”

WebEngine/WebView

Streaming API

JavaScript call createBall()

Page 37: [English version] JavaFX and Web Integration

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

Summary

• JavaFX has powerful Web Component

– WebKit based full-fledged built-in browser

• Partial HTML5 support

• High performance

– APIs to integrate Java and Web apps

• JavaScript bridge

• DOM access

⇒You can produce new “mash-up”s of Web and Java!

JavaFX and Web Integration

Page 38: [English version] JavaFX and Web Integration

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

Acknowledgement

• This presentation is mostly based on:

– TS24313 “JavaFX 2.0 Web Component at a Glance”

• JavaOne 2011 San Francisco

• by Per Bothner, Peter Zhelezniakov

– The JavaFX Blog: “Communicating between JavaScript and

JavaFX with WebEngine”

• https://blogs.oracle.com/javafx/entry/communicating_between_javascri

pt_and_javafx

• by Peter Zhelezniakov

Page 39: [English version] JavaFX and Web Integration

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

Page 40: [English version] JavaFX and Web Integration

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