Tech and Media Labs
This site uses cookies to improve the user experience.


JavaFX WebView

Jakob Jenkov
Last update: 2018-10-10

The JavaFX WebView (javafx.scene.web.WebView) component is capable of showing web pages (HTML, CSS, SVG, JavaScript) inside a JavaFX application. As such, the JavaFX WebView is a mini browser. The WebView component is very handy when you need to show documentation (e.g. Help texts), news, blog posts or other content which needs to be downloaded from a web server at runtime.

JavaFX WebView Example

The WebView component is a JavaFX Node so it can be included in the scene graph like any other JavaFX component which is also a Node. Here is a simple JavaFX WebView example:

package com.jenkov.javafx;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX WebView Example");

        WebView webView = new WebView();

        webView.getEngine().load("http://google.com");

        VBox vBox = new VBox(webView);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();

    }
}

This example shows a JavaFX application that creates a WebView which is inserted into a JavaFX VBox layout component - which is again placed inside a JavaFX Stage - which is set on the primary Stage.

WebView WebEngine

The JavaFX WebView WebEngine (javafx.scene.web.WebEngine) is an internal component used by the WebView to load the data that is to be displayed inside the WebView. To make the WebView WebEngine load data, you must first obtain the WebEngine instance from the WebView.

Obtaining the WebEngine

You obtain the WebEngine from the WebView by calling the WebView getEngine() method. Here is an example of obtaining the WebEngine from a JavaFX WebView:

WebView webView = new WebView();

WebEngine webEngine = webView.getEngine();

Loading a Web Page

Once you have obtained a WebEngine instance from the WebView you can load data by calling its load() method. The load() method takes a URL as parameter. Here is an example of loading a web page via the WebEngine load() method:

webEngine.load("http://google.com");

Loading Local Content

The WebView WebEngine can load local content too - meaning content that is supplied to it directly in a method call (not loaded over the Internet). The WebEngine loads local content via the loadContent() method. Here is an example of loading local contain in a JavaFX WebView using loadContent()

String content =
    "Hello World!";

webEngine.loadContent(content, "text/html");

The first parameter to the loadContent() call in the example above is the content itself. In this example it is a very simple HTML document contained in a Java String.

The second parameter to the loadContent() call in the example above is the content type (mime type) of the content. Since we are loading an HTML document, the standard content type for that is text/html.

More coming soon...

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC