In this blog, we will be looking at how JavaFX can render webpages and the component responsible for it – which is known as WebView
JavaFX is a:
- Software platform for creating and delivering desktop applications, as well as rich internet applications (RIAs) that can run across a wide variety of devices.
- Set of graphics and media packages that enables developers to design, create, test, debug, and deploy rich client applications that operate consistently across diverse platforms.
JavaFX Key Features:
- JavaFX WebView is a mini browser (also called as an embedded browser) that provides a web viewer and full browsing functionality through its API in JavaFX applications.
- The WebView class is an extension of the Node class.
- The embedded browser inherits all fields and methods from the Node class, and therefore, it has all its features.
- It encapsulates a WebEngine object, incorporates HTML content into an application's scene, and provides properties and methods to apply effects and transformations.
- The getEngine() method called on a WebView object returns a Web Engine associated with it.
- The classes that constitute the embedded browser reside in the javafx.scene.web package.
- WebView enables developers to implement the following features in their Java applications:
- Render HTML content from local or remote URLs
- Support history and provide Back and Forward navigation
- Reload the content
- Apply effects to the web component
- Edit the HTML content
- Handle events
- WebView component supports the following HTML5 features apart from supporting CSS3 and ecmascript6 (ES6):
- Media Playback
- Form controls (except for <input type="color"> )
- Editable content
- History maintenance
- Support for the <meter>, <progress>, <details> and <summary> tags
- Web Sockets
- Web Workers
- Support for domain names written in national languages
Below diagram depicts architecture of embedded browser and its relation to other JavaFX classes:
- is a non-visual object capable of managing one Web page at a time
- Provides basic web page functionality through its API.
- It supports user interaction such as navigating links and submitting HTML forms, although it does not interact with users directly.
- It wraps a WebPage object, which provides interaction with the native Webkit core.
Relationship between WebView and WebEngine classes:
Code Snippet for Loading content in JavaFX WebView:
- Create WebView, WebEngine objects and load via Remote URL.:
2. Load Static HTML Content:
3. Loading HTML content from local file:
4. To track Load Progress with the help of LoadWorker:
- Loading always happens on a background thread. Methods that initiate loading return immediately after scheduling a background job.
- To track progress and/or cancel a job, we can use the Worker instance available from the getLoadWorker() method.
- The following example changes the stage title when loading completes successfully:
5. Access to Document Model
- The WebEngine objects create and manage a Document Object Model (DOM) for their Web pages. The model can be accessed and modified using Java DOM Core classes.
- The getDocument() method provides access to the root of the model. Additionally DOM Event specification is supported to define event handlers in Java code.
- The following example attaches a Java event listener to an element of a Web page. Clicking on the element causes the application to exit:
- The most commonly used JSObject methods are getMember (to read a named property), setMember (to set or define a property), and call (to call a function-valued property).
- A DOM Node is mapped to an object that both extends JSObject and implements the appropriate DOM interfaces. To get a JSObject object for a Node just do a cast:
JSObject jdoc = (JSObject) webEngine.getDocument();
Webkit upgrade in JDK:
- WebView Webkit engine is based Apple Safari Port (https://trac.webkit.org/). This port also being used by iOS, GTK, WinCairo, EFL etc..
- We follow Webkit GTK release cycle and will upgrade Webkit once in 6 months ( https://trac.webkit.org/wiki/WebKitGTK/StableRelease )
- The purpose of upgrading Webkit is to have security vulnerability fixes apart from new features and adhering to latest standards.
My next few blogs will cover details of JavaFX WebView architecture and internals of Webkit engine.