JxBrowser and JavaFX WebView
Choosing a JavaFX web browser control for embedding into a cross-platform desktop application
Join the DZone community and get the full member experience.
Join For FreeBoth JxBrowser and JavaFX WebView allow you to bring web technologies in your cross-platform Java desktop application to get the best of both worlds: the ubiquity of the web platform and the power of the Java platform.
What's the difference between JxBrowser and JavaFX WebView? In this article we will compare these solutions in terms of their architecture, rendering, support of the latest web standards including HTML5, CSS3, JavaScript, and more.
Installation
JavaFX WebView was introduced in JavaFX 2.0 that became a part of JDK 8 in 2014. If you use Java 8, then you don’t need to do anything special to start using JavaFX WebView. With JDK 11 and higher JavaFX is no longer bundled, so to develop using JavaFX 11 or higher you must download it separately.
JxBrowser is a third-party library that you need to add to your project as a dependency. You can simply download and add it to your application classpath, or quickly add it to your Gradle or Maven project.
Engine
JavaFX WebView uses WebKit internally. JxBrowser uses a fully-functional Chromium underhood. Both solutions include all the necessary engine binaries and do not require the end users to install Chromium or Safari.
Rendering
WebKit does not render web pages, so JavaFX developers had to implement a separate lightweight (off-screen) renderer. JavaFX 17 WebView renders web pages well, so there won’t be any visible differences compared with Safari, Mozilla Firefox, or Google Chrome.
In JxBrowser the web pages are rendered by Chromium using GPU. So they look exactly as in Google Chrome. JxBrowser supports two rendering modes: off-screen and hardware accelerated.
Architecture
JavaFX initializes and runs WebKit in your Java process. The advantage of this approach is that JavaFX allows you to create and display WebView very quickly compared to JxBrowser, where an external Chromium process must be launched first. However, to obtain such a speed WebKit allocates and uses the memory and CPU of your Java process. Some modern web pages might allocate more than 1GB of RAM. The more WebView instances you create and load web pages, the more RAM of your Java application will be taken.
Security
WebKit is written using C++. To call WebKit functionality, JavaFX WebView uses JNI. Since WebKit is running inside JVM, any error or unexpected behavior might cause a JVM crash and terminate your Java application unexpectedly. The end user’s data might be lost or corrupted.
In case of an error in Chromium, your Java program will continue running. JxBrowser even provides an API to inform you about unexpected Chromium termination or crash in a Chromium process, so you can re-initialize and restore a user session for a better user experience.
Web Standards
If you load http://html5test.com in JavaFX 17 WebView and JxBrowser 7.20 (Chromium 94) you will get the following results:
And renders Google with some artefacts:
JavaFX 17 works well for the same tasks:
WebGL is not supported by JavaFX.
Supported UI-Toolkits
JavaFX WebView can be seamlessly used for JavaFX applications. If you develop your app using Java Swing or SWT, then you can embed JavaFX WebView via JFXPanel and FXCanvas.
JxBrowser supports all Java UI-toolkits including JavaFX, Swing, and SWT. It provides the following components for embedding:
com.teamdev.jxbrowser.view.swing.BrowserView
com.teamdev.jxbrowser.view.javafx.BrowserView
com.teamdev.jxbrowser.view.swt.BrowserView
Embedding
The efforts required to write a simple JavaFX app with an address bar and web view that renders the currently loaded web page are pretty similar.
JavaFX WebView
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public final class Main extends Application {
@Override
public void start(Stage primaryStage) {
WebView view = new WebView();
WebEngine engine = view.getEngine();
BorderPane root = new BorderPane(view);
TextField addressBar = new TextField("https://google.com");
addressBar.setOnAction(event ->
engine.load(addressBar.getText()));
root.setTop(addressBar);
// Update address bar with URL of the loaded web page.
engine.locationProperty().addListener((observable, oldValue, newValue) ->
addressBar.setText(newValue));
primaryStage.setTitle("JavaFX WebView");
primaryStage.setScene(new Scene(root, 1000, 600));
primaryStage.show();
engine.load(addressBar.getText());
}
}
JxBrowser
import static com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED;
import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.navigation.event.NavigationFinished;
import com.teamdev.jxbrowser.view.javafx.BrowserView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public final class SmokeTest extends Application {
@Override
public void start(Stage primaryStage) {
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
BrowserView view = BrowserView.newInstance(browser);
BorderPane root = new BorderPane(view);
TextField addressBar = new TextField("https://google.com");
addressBar.setOnAction(event ->
browser.navigation().loadUrl(addressBar.getText()));
root.setTop(addressBar);
// Update address bar with URL of the loaded web page.
browser.navigation().on(NavigationFinished.class, event -> {
if (event.isInMainFrame()) {
addressBar.setText(event.url());
}
});
primaryStage.setTitle("JxBrowser");
primaryStage.setScene(new Scene(root, 1000, 600));
primaryStage.show();
browser.navigation().loadUrl(addressBar.getText());
// Close the engine when stage is about to close.
primaryStage.setOnCloseRequest(event -> engine.close());
}
}
API and Features
JavaFX WebView API: ~1 package, ~10 classes & interfaces. JavaFX WebView represents a simple web browser control. There were no goals to provide the functionality of a fully-functional web browser such as network, SSL, authentication, cookies, DevTools, plugins, PDF Viewer, etc.
The API provides the basic web browser functionality that allows loading a web page, getting various load events, accessing and modifying DOM, executing JavaScript, calling Java from JavaScript by injecting Java objects into JavaScript, handling popups, and displaying JavaScript dialogs such as alert, confirm, prompt.
JxBrowser API: ~60 packages, ~580 classes & interfaces. JxBrowser wraps and uses a fully-functional web browser app underhood.
The API provides access to hundreds of the Chromium features such as managing profiles including incognito, plugins, proxy, cookies, spell checking, downloads, permissions, authentication (proxy, basic, digest, NTLM, SSL client certificates, SuisseID, U2F, Integrated Windows Authentication and Kerberos), passwords, zoom, printing, DevTools, network, custom protocols and more.
Support and Updates
JavaFX is an open source project. If you see a bug or there is a missing feature, you can contribute. JavaFX follows OpenJDKs 6-months release cycle. A new version is released every 6 months. It is still unknown how often the WebKit engine is upgraded to the latest stable version that supports the latest web standards and includes the fixes of the reported security vulnerabilities.
JxBrowser is a commercial product designed and created for commercial companies that have high requirements for the quality and support of integrated third-party solutions, and develop software using Java technology. JxBrowser has been developed and supported by TeamDev since 2007.
All the clients with an active Standard Support subscription can use all new JxBrowser versions for free and receive technical support from JxBrowser engineers directly.
A new version of JxBrowser is released almost every month.
Summary
If you develop your software using JavaFX and you need to display a simple web page with no need to use HTML5 features or advanced web browser functionality, JavaFX WebView will work well for the case.
For time-sensitive commercial software development on JavaFX, Swing, or SWT, where modern web functionality is necessary for specific use cases, and compliance with the latest web standards is a must, JxBrowser is a safer and more customisable solution, backed by the full-time development team.
Published at DZone with permission of Vladimir Ikryanov. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments