SlideShare a Scribd company logo
1   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
JavaFX and Web Integration
            Kazuchika Sekiya
2           Java Embedded Global Business Unit, Oracle Japan
    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
                                                                           English version
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.




3   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Servers                            Desktop     Embedded      TV        Mobile     Card
                                                                                            BD-J
    Key APIs                    Java EE                              JavaFX               Java TV     MSA

    Platform                                                Java SE                         Java ME            Java Card

Language                                                                         Java Language
                                                                                 Java Platform
4    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
What is JavaFX?
    Next Generation Java Client Solution




                  Media                                                                                     Web

             Audio/Video                                                                            HTML5/CSS3/JavaScript



                                                                                               Animations
                                                                                               Effects
                                                                           Advanced Graphics   3D
5   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
JavaFX 2.0 Architecture




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

    • Overview
    • API: WebEngine and WebView
    • Advanced Features




7   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




8   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




9   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




10   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Performance
     Windows
     • GUIMark2 HTML5 test http://www.craftymind.com/guimark2

                                                                            Vector Test      Bitmap Test           Text Test
                    Chrome
                                                                                    16.4                22.1                20.6
                    17.0
                    Firefox
                                                                                    12.2                 9.8                28.4
                    10.0.2
                    JavaFX
                                                                                    15.1                30.2                 5.8
                    2.0.3
                                                                      Windows 7 Professional / Intel Core 2 Duo 2.53GHz / RAM 4.0GB
11   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Performance
     Mac
     • GUIMark2 HTML5 test http://www.craftymind.com/guimark2

                                                                            Vector Test     Bitmap Test          Text Test
                    Chrome
                                                                                   16.9                 56                18.9
                    18.0
                    Safari
                                                                                     3.5              14.5                23.8
                    5.1.5
                    JavaFX
                                                                                   15.5               31.1                 7.0
                    2.1 beta b19
                                                                            Mac OS X 10.6.8 / Intel Core 2 Duo 2.13GHz / RAM 4.0GB
12   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
DEMO


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

     • Overview
     • API: WebEngine and WebView
     • Advanced Features




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




15   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
WebEngine
     package javafx.scene.web;
     • Non visual component
     • Provides following functions:
           – Loads Web pages
           – Runs scripts on pages
           – Manages DOM
     • Can be used without WebView




16   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
WebView
     package javafx.scene.web;
     • Is a scene graph Node
           – Used to display Web pages
     • Has an associated WebEngine
           – Created at construction time
           – Cannot be changed
           – WebView.getEngine()




17   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




18   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Tracking of Web Page Loading
     package javafx.concurrent;
     • Loading is asynchronous
     • Use WebEngine.getLoadWorker() to track loading
           – state
                    • READY -> SCHEDULED -> RUNNING ->
                      SUCCEEDED/FAILED/CANCELED
           – progress (0…1)
           – workDone / totalWork
           – Can cancel() loading



19   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");



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


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

     • Overview
     • API: WebEngine and WebView
     • Advanced Features




22   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!




23   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);




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


25   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)



 26   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");
}




  27   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


28   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
JSObject
     package netscape.javascript;
     • Java wrapper around JavaScript object
     • Part of LiveConnect API

     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


29   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");




30   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




31   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Calling Java from JavaScript

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



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


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

  32   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




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


34   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Control Google Map using Wii Remote


                                                                             WiiRemoteJ

                                                                              BlueCove               Google Map
                             Bluetooth


                                                                                 JavaScript API
                                                                                      call



                                                                                                  WebEngine/WebView
Wii Remote

 35   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Integrate Twitter4J library and HTML5 App



                                                                                                 HTML5 app
                                                                                                 “Ball Pool”
     Streaming API
                                                                            JavaScript call
                                                                             createBall()




                                                                                              WebEngine/WebView


36   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Summary
     JavaFX and Web Integration
     • 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!


37   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


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

More Related Content

PPTX
JavaFX Versus HTML5 - JavaOne 2014
PDF
Java Framework comparison
PPTX
VisionX Prototyping.
PPTX
JavaFX 2 Using the Spring Framework
PPT
Developing modular Java applications
PDF
The Modern Java Web Developer - Denver JUG 2013
PDF
Apache Cordova 4.x
PDF
The web - What it has, what it lacks and where it must go - Istanbul
JavaFX Versus HTML5 - JavaOne 2014
Java Framework comparison
VisionX Prototyping.
JavaFX 2 Using the Spring Framework
Developing modular Java applications
The Modern Java Web Developer - Denver JUG 2013
Apache Cordova 4.x
The web - What it has, what it lacks and where it must go - Istanbul

What's hot (20)

PDF
Java EE 8: On the Horizon
PDF
Modern web application development with java ee 7
PDF
JavaCro'15 - Java EE 8 - An instant snapshot - David Delabassee
PDF
EuroPython 2011 - How to build complex web applications having fun?
PDF
Java EE 8 Adopt a JSR : JSON-P 1.1 & MVC 1.0
PDF
Java EE Revisits GoF Design Patterns
PDF
The Modern Java Web Developer - JavaOne 2013
PDF
Advance Java Tutorial | J2EE, Java Servlets, JSP, JDBC | Java Certification T...
PDF
JavaCro'15 - HTTP2 Comes to Java! - David Delabassee
PDF
HTML5 and the dawn of rich mobile web applications
PPT
Java EE and Spring Side-by-Side
PDF
Java Modularity: the Year After
PPTX
Best Practices for JSF, Gameduell 2013
PPTX
MWLUG - Universal Java
PDF
5 best Java Frameworks
KEY
Agile toolkit present 2012
PPT
01 java intro
PDF
Hybrid application development
PDF
Wicket Deliver Your Webapp On Time
Java EE 8: On the Horizon
Modern web application development with java ee 7
JavaCro'15 - Java EE 8 - An instant snapshot - David Delabassee
EuroPython 2011 - How to build complex web applications having fun?
Java EE 8 Adopt a JSR : JSON-P 1.1 & MVC 1.0
Java EE Revisits GoF Design Patterns
The Modern Java Web Developer - JavaOne 2013
Advance Java Tutorial | J2EE, Java Servlets, JSP, JDBC | Java Certification T...
JavaCro'15 - HTTP2 Comes to Java! - David Delabassee
HTML5 and the dawn of rich mobile web applications
Java EE and Spring Side-by-Side
Java Modularity: the Year After
Best Practices for JSF, Gameduell 2013
MWLUG - Universal Java
5 best Java Frameworks
Agile toolkit present 2012
01 java intro
Hybrid application development
Wicket Deliver Your Webapp On Time
Ad

Viewers also liked (16)

PDF
G* Workshop in Fukuoka - Introduction
PDF
お題でGroovyプログラミング: Part A
PDF
JGGUG合宿2011報告
PDF
API Meetupのこれまでとこれから
PPTX
シェアリングエコノミー推進に係る政府の取り組について(犬童周作)
PDF
minikura API がもたらした“予想外”な価値・課題
PPTX
APIエコノミーの現状と今後の期待
PPTX
Routeサービスを使ったCloud FoundryアプリのAPI管理
PDF
OpenAPI Specification概要
PDF
Apigee+OASでらくらくAPI開発(予定)
PDF
NHK Linked Data API 〜つながる番組データを目指して〜
PDF
[Azure Deep Dive] APIエコノミーに向けて ~Azure API ManagementによるAPIの公開と管理~ (2016/12/16)
PPTX
JavaStart - kurs Java Podstawy
PDF
APIdays Australia 2017 TOI #APIdaysAU
PDF
Introducing Ballerina
G* Workshop in Fukuoka - Introduction
お題でGroovyプログラミング: Part A
JGGUG合宿2011報告
API Meetupのこれまでとこれから
シェアリングエコノミー推進に係る政府の取り組について(犬童周作)
minikura API がもたらした“予想外”な価値・課題
APIエコノミーの現状と今後の期待
Routeサービスを使ったCloud FoundryアプリのAPI管理
OpenAPI Specification概要
Apigee+OASでらくらくAPI開発(予定)
NHK Linked Data API 〜つながる番組データを目指して〜
[Azure Deep Dive] APIエコノミーに向けて ~Azure API ManagementによるAPIの公開と管理~ (2016/12/16)
JavaStart - kurs Java Podstawy
APIdays Australia 2017 TOI #APIdaysAU
Introducing Ballerina
Ad

Similar to [English version] JavaFX and Web Integration (20)

PDF
How Scala, Wicket, and Java EE Can Improve Web Development
PPT
What's New in WebLogic 12.1.3 and Beyond
PPTX
Moving to the Client - JavaFX and HTML5
PDF
Jspx Jdc2010
PDF
JavaOne Update zur Java Plattform
PDF
Java keynote preso
PDF
JavaFX 2 Rich Desktop Platform
PDF
Why should i switch to Java SE 7
PDF
Development with JavaFX 9 in JDK 9.0.1
PDF
Java 2012 conference keynote - Java Strategy & Roadmap - WebLogic & GlassFish...
PDF
Coding for Desktop and Mobile with HTML5 and Java EE 7
PDF
B1 roadmap to cloud platform with oracle web logic server-oracle coherence ...
PPTX
WebLogic Developer Experience and Java EE 6
PDF
Java Cloud and Container Ready
PPT
Ugf9796 weblogic for ebs and obiee
PDF
JavaOne 2010 Keynote
PPT
Developing Java Web Applications
PDF
Building Java Desktop Apps with JavaFX 8 and Java EE 7
PDF
Paper published on web application testing with sahi tool
PDF
JavaCro'14 - WebLogic-GlassFish-JaaS Strategy and Roadmap – Duško Vukmanović
How Scala, Wicket, and Java EE Can Improve Web Development
What's New in WebLogic 12.1.3 and Beyond
Moving to the Client - JavaFX and HTML5
Jspx Jdc2010
JavaOne Update zur Java Plattform
Java keynote preso
JavaFX 2 Rich Desktop Platform
Why should i switch to Java SE 7
Development with JavaFX 9 in JDK 9.0.1
Java 2012 conference keynote - Java Strategy & Roadmap - WebLogic & GlassFish...
Coding for Desktop and Mobile with HTML5 and Java EE 7
B1 roadmap to cloud platform with oracle web logic server-oracle coherence ...
WebLogic Developer Experience and Java EE 6
Java Cloud and Container Ready
Ugf9796 weblogic for ebs and obiee
JavaOne 2010 Keynote
Developing Java Web Applications
Building Java Desktop Apps with JavaFX 8 and Java EE 7
Paper published on web application testing with sahi tool
JavaCro'14 - WebLogic-GlassFish-JaaS Strategy and Roadmap – Duško Vukmanović

More from Kazuchika Sekiya (11)

PPTX
10分でわかるOpenAPI V3
PDF
Apigee x Drupal: APIエコノミーを支える開発者ポータル
PDF
[JavaOne Tokyo 2012] JavaFX and Web Integration
PDF
KEY
「プログラミングGroovy」発売予告
PDF
「プログラミングGroovy」Groovyってなんだろ?編
PDF
PDF
"G"はGrapeのG
PDF
GroovyなGAE/J Gaelykでかんたんbot工作
PDF
Groovy/Grails on Google App Engine <シンプル導入編>
10分でわかるOpenAPI V3
Apigee x Drupal: APIエコノミーを支える開発者ポータル
[JavaOne Tokyo 2012] JavaFX and Web Integration
「プログラミングGroovy」発売予告
「プログラミングGroovy」Groovyってなんだろ?編
"G"はGrapeのG
GroovyなGAE/J Gaelykでかんたんbot工作
Groovy/Grails on Google App Engine <シンプル導入編>

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPT
Teaching material agriculture food technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
cuic standard and advanced reporting.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Big Data Technologies - Introduction.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Cloud computing and distributed systems.
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
MYSQL Presentation for SQL database connectivity
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Teaching material agriculture food technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Encapsulation_ Review paper, used for researhc scholars
cuic standard and advanced reporting.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Empathic Computing: Creating Shared Understanding
Big Data Technologies - Introduction.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Digital-Transformation-Roadmap-for-Companies.pptx
Machine learning based COVID-19 study performance prediction
Mobile App Security Testing_ A Comprehensive Guide.pdf
Cloud computing and distributed systems.
Agricultural_Statistics_at_a_Glance_2022_0.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Understanding_Digital_Forensics_Presentation.pptx
sap open course for s4hana steps from ECC to s4
MYSQL Presentation for SQL database connectivity

[English version] JavaFX and Web Integration

  • 1. 1 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 2. JavaFX and Web Integration Kazuchika Sekiya 2 Java Embedded Global Business Unit, Oracle Japan Copyright © 2012, Oracle and/or its affiliates. All rights reserved. English version
  • 3. 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. 3 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 4. Servers Desktop Embedded TV Mobile Card BD-J Key APIs Java EE JavaFX Java TV MSA Platform Java SE Java ME Java Card Language Java Language Java Platform 4 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 5. What is JavaFX? Next Generation Java Client Solution Media Web Audio/Video HTML5/CSS3/JavaScript Animations Effects Advanced Graphics 3D 5 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 6. JavaFX 2.0 Architecture 6 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 7. Agenda • Overview • API: WebEngine and WebView • Advanced Features 7 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 8. JavaFX Web Component • Full-fledged built-in browser – HTML4, partial HTML5 support – JavaScript engine – LiveConnect – DOM access – SVG support 8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 9. 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 9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 10. 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 10 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 11. Performance Windows • GUIMark2 HTML5 test http://www.craftymind.com/guimark2 Vector Test Bitmap Test Text Test Chrome 16.4 22.1 20.6 17.0 Firefox 12.2 9.8 28.4 10.0.2 JavaFX 15.1 30.2 5.8 2.0.3 Windows 7 Professional / Intel Core 2 Duo 2.53GHz / RAM 4.0GB 11 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 12. Performance Mac • GUIMark2 HTML5 test http://www.craftymind.com/guimark2 Vector Test Bitmap Test Text Test Chrome 16.9 56 18.9 18.0 Safari 3.5 14.5 23.8 5.1.5 JavaFX 15.5 31.1 7.0 2.1 beta b19 Mac OS X 10.6.8 / Intel Core 2 Duo 2.13GHz / RAM 4.0GB 12 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 13. DEMO 13 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 14. Agenda • Overview • API: WebEngine and WebView • Advanced Features 14 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 15. WebEngine and WebView 15 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 16. WebEngine package javafx.scene.web; • Non visual component • Provides following functions: – Loads Web pages – Runs scripts on pages – Manages DOM • Can be used without WebView 16 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 17. WebView package javafx.scene.web; • Is a scene graph Node – Used to display Web pages • Has an associated WebEngine – Created at construction time – Cannot be changed – WebView.getEngine() 17 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 18. 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 18 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 19. Tracking of Web Page Loading package javafx.concurrent; • Loading is asynchronous • Use WebEngine.getLoadWorker() to track loading – state • READY -> SCHEDULED -> RUNNING -> SUCCEEDED/FAILED/CANCELED – progress (0…1) – workDone / totalWork – Can cancel() loading 19 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 20. 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"); 20 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 21. DEMO 21 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 22. Agenda • Overview • API: WebEngine and WebView • Advanced Features 22 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 23. 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! 23 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 24. 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); 24 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 25. DEMO 25 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 26. 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) 26 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 27. 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"); } 27 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 28. 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 28 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 29. JSObject package netscape.javascript; • Java wrapper around JavaScript object • Part of LiveConnect API 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 29 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 30. 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"); 30 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 31. 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 31 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 32. Calling Java from JavaScript class Bridge { public void exit() { Platform.exit(); } } JSObject jsobj = (JSObject)webEngine.executeScript("window"); jsobj.setMember("java", new Bridge()); <p>Click <a href="" onclick="java.exit();">here</a> to exit the application</p> 32 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 33. 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 33 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 34. DEMO 34 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 35. Control Google Map using Wii Remote WiiRemoteJ BlueCove Google Map Bluetooth JavaScript API call WebEngine/WebView Wii Remote 35 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 36. Integrate Twitter4J library and HTML5 App HTML5 app “Ball Pool” Streaming API JavaScript call createBall() WebEngine/WebView 36 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 37. Summary JavaFX and Web Integration • 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! 37 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 38. 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 38 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 39. 39 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 40. 40 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.