Top Description Inners Fields Constructors Methods
javafx.scene.web

public final Class WebEngine

extends Object
Class Inheritance
Imports
com.sun.javafx.logging.PlatformLogger, com.sun.javafx.scene.web.Debugger, .Printable, com.sun.javafx.tk.TKPulseListener, .Toolkit, com.sun.javafx.webkit.*, com.sun.javafx.webkit.prism.PrismGraphicsManager, .PrismInvoker, com.sun.javafx.webkit.prism.theme.PrismRenderer, com.sun.javafx.webkit.theme.RenderThemeImpl, .Renderer, com.sun.webkit.*, com.sun.webkit.graphics.WCGraphicsManager, com.sun.webkit.network.URLs, .Util, javafx.animation.AnimationTimer, javafx.application.Platform, javafx.beans.InvalidationListener, javafx.beans.property.*, javafx.concurrent.Worker, javafx.event.EventHandler, .EventType, javafx.geometry.Rectangle2D, javafx.print.JobSettings, .PageLayout, .PageRange, .PrinterJob, javafx.scene.Node, javafx.util.Callback, org.w3c.dom.Document, java.io.BufferedInputStream, .File, .IOException, java.lang.ref.WeakReference, java.net.MalformedURLException, .URLConnection, java.nio.file.Files, .Path, java.nio.file.attribute.PosixFilePermissions, java.util.ArrayList, .Base64, .List, .Objects

WebEngine is a non-visual object capable of managing one Web page at a time. It loads Web pages, creates their document models, applies styles as necessary, and runs JavaScript on pages. It provides access to the document model of the current page, and enables two-way communication between a Java application and JavaScript code of the page.

Loading Web Pages

The WebEngine class provides two ways to load content into a WebEngine object:

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, use the javafx.concurrent.Worker instance available from the getLoadWorker method.

The following example changes the stage title when loading completes successfully:

import javafx.concurrent.Worker.State;
    final Stage stage;
    webEngine.getLoadWorker().stateProperty().addListener(
        new ChangeListener<State>() {
            public void changed(ObservableValue ov, State oldState, State newState) {
                if (newState == State.SUCCEEDED) {
                    stage.setTitle(webEngine.getLocation());
                }
            }
        });
    webEngine.load("http://javafx.com");
 

User Interface Callbacks

A number of user interface callbacks may be registered with a WebEngine object. These callbacks are invoked when a script running on the page requests a user interface operation to be performed, for example, opens a popup window or changes status text. A WebEngine object cannot handle such requests internally, so it passes the request to the corresponding callbacks. If no callback is defined for a specific operation, the request is silently ignored.

The table below shows JavaScript user interface methods and properties with their corresponding WebEngine callbacks:

JavaScript Callback Table
JavaScript method/property WebEngine callback
window.alert()onAlert
window.confirm()confirmHandler
window.open()createPopupHandler
window.open() and
window.close()
onVisibilityChanged
window.prompt()promptHandler
Setting window.statusonStatusChanged
Setting any of the following:
window.innerWidth, window.innerHeight,
window.outerWidth, window.outerHeight,
window.screenX, window.screenY,
window.screenLeft, window.screenTop
onResized

The following example shows a callback that resizes a browser window:

Stage stage;
    webEngine.setOnResized(
        new EventHandler<WebEvent<Rectangle2D>>() {
            public void handle(WebEvent<Rectangle2D> ev) {
                Rectangle2D r = ev.getData();
                stage.setWidth(r.getWidth());
                stage.setHeight(r.getHeight());
            }
        });
 

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:

EventListener listener = new EventListener() {
        public void handleEvent(Event ev) {
            Platform.exit();
        }
    };

    Document doc = webEngine.getDocument();
    Element el = doc.getElementById("exit-app");
    ((EventTarget) el).addEventListener("click", listener, false);
 

Evaluating JavaScript expressions

It is possible to execute arbitrary JavaScript code in the context of the current page using the executeScript method. For example:

webEngine.executeScript("history.back()");
 

The execution result is returned to the caller, as described in the next section.

Mapping JavaScript values to Java objects

JavaScript values are represented using the obvious Java classes: null becomes Java null; a boolean becomes a java.lang.Boolean; and a string becomes a java.lang.String. A number can be java.lang.Double or a java.lang.Integer, depending. The undefined value maps to a specific unique String object whose value is "undefined".

If the result is a JavaScript object, it is wrapped as an instance of the netscape.javascript.JSObject class. (As a special case, if the JavaScript object is a JavaRuntimeObject as discussed in the next section, then the original Java object is extracted instead.) The JSObject class is a proxy that provides access to methods and properties of its underlying JavaScript object. 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();
 

In some cases the context provides a specific Java type that guides the conversion. For example if setting a Java String field from a JavaScript expression, then the JavaScript value is converted to a string.

Mapping Java objects to JavaScript values

The arguments of the JSObject methods setMember and call pass Java objects to the JavaScript environment. This is roughly the inverse of the JavaScript-to-Java mapping described above: Java String, Number, or Boolean objects are converted to the obvious JavaScript values. A JSObject object is converted to the original wrapped JavaScript object. Otherwise a JavaRuntimeObject is created. This is a JavaScript object that acts as a proxy for the Java object, in that accessing properties of the JavaRuntimeObject causes the Java field or method with the same name to be accessed.

Note that the Java objects bound using JSObject.setMember, JSObject.setSlot, and JSObject.call are implemented using weak references. This means that the Java object can be garbage collected, causing subsequent accesses to the JavaScript objects to have no effect.

Calling back to Java from JavaScript

The JSObject.setMember method is useful to enable upcalls from JavaScript into Java code, as illustrated by the following example. The Java code establishes a new JavaScript object named app. This object has one public member, the method exit.


public class JavaApplication {
    public void exit() {
        Platform.exit();
    }
}
...
JavaApplication javaApp = new JavaApplication();
JSObject window = (JSObject) webEngine.executeScript("window");
window.setMember("app", javaApp);
 
You can then refer to the object and the method from your HTML page:
<a href="" onclick="app.exit()">Click here to exit application</a>
 

When a user clicks the link the application is closed.

Note that in the above example, the application holds a reference to the JavaApplication instance. This is required for the callback from JavaScript to execute the desired method.

In the following example, the application does not hold a reference to the Java object:


 JSObject window = (JSObject) webEngine.executeScript("window");
 window.setMember("app", new JavaApplication());
 

In this case, since the property value is a local object, "new JavaApplication()", the value may be garbage collected in next GC cycle.

When a user clicks the link, it does not guarantee to execute the callback method exit.

If there are multiple Java methods with the given name, then the engine selects one matching the number of parameters in the call. (Varargs are not handled.) An unspecified one is chosen if there are multiple ones with the correct number of parameters.

You can pick a specific overloaded method by listing the parameter types in an "extended method name", which has the form "method_name(param_type1,...,param_typen)". Typically you'd write the JavaScript expression:

 receiver["method_name(param_type1,...,param_typeN)"](arg1,...,argN)
 

The Java class and method must both be declared public.

Deploying an Application as a Module

If any Java class passed to JavaScript is in a named module, then it must be reflectively accessible to the javafx.web module. A class is reflectively accessible if the module opens the containing package to at least the javafx.web module. Otherwise, the method will not be called, and no error or warning will be produced.

For example, if com.foo.MyClass is in the foo.app module, the module-info.java might look like this:

module foo.app {
    opens com.foo to javafx.web;
}

Alternatively, a class is reflectively accessible if the module exports the containing package unconditionally.

Starting with JavaFX 14, HTTP/2 support has been added to WebEngine. This is achieved by using java.net.http.HttpClient instead of URLConnection. HTTP/2 is activated by default when JavaFX 14 (or later) is used with JDK 12 (or later).

Threading

WebEngine objects must be created and accessed solely from the JavaFX Application thread. This rule also applies to any DOM and JavaScript objects obtained from the WebEngine object.

Since
JavaFX 2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private class
WebEngine.DebuggerImpl

The debugger implementation.

private class
private static class
WebEngine.InspectorClientImpl

The inspector client implementation.

private class
private static class
WebEngine.PageLoadListener

The page load event listener.

private static class
WebEngine.PulseTimer

Drives the Timer when Timer.Mode.PLATFORM_TICKS is set.

private static class

Field Summary

Modifier and TypeField and Description
private final ObjectProperty<Callback<String, Boolean>>
private final ObjectProperty<Callback<PopupFeatures, WebEngine>>
private final WebEngine.DebuggerImpl
private final WebEngine.SelfDisposer
private final WebEngine.DocumentProperty
private final WebHistory
private static int
instanceCount

The number of instances of this class.

private BooleanProperty
javaScriptEnabled

Specifies whether JavaScript execution is enabled.

private final WebEngine.LoadWorker
loadWorker

The Worker which shows progress of the web engine as it loads pages.

private final ReadOnlyStringWrapper
private static final PlatformLogger
private final ObjectProperty<EventHandler<WebEvent<String>>>
private final ObjectProperty<EventHandler<WebErrorEvent>>
onError

The event handler called when an error occurs.

private final ObjectProperty<EventHandler<WebEvent<Rectangle2D>>>
private final ObjectProperty<EventHandler<WebEvent<String>>>
private final ObjectProperty<EventHandler<WebEvent<Boolean>>>
private final WebPage
page

The object that provides interaction with the native webkit core.

private final ObjectProperty<Callback<PromptData, String>>
private final ReadOnlyStringWrapper
private StringProperty
userAgent

Specifies user agent ID string.

private final ObjectProperty<File>
userDataDirectory

Specifies the directory to be used by this WebEngine to store local user data.

private boolean
private StringProperty
userStyleSheetLocation

Location of the user stylesheet as a string URL.

private final ObjectProperty<WebView>
view

The node associated with this engine.

Constructor Summary

AccessConstructor and Description
public
WebEngine()

Creates a new engine.

public
WebEngine(String
the URL of the web page to load
url
)

Creates a new engine and loads a Web page into it.

private
WebEngine(String url, boolean callLoad)

Method Summary

Modifier and TypeMethod and Description
private void
pack-priv static void
public final ObjectProperty<Callback<String, Boolean>>

Returns:

the confirmHandler property
confirmHandlerProperty
()

JavaScript confirm handler property.

private static void
public final ObjectProperty<Callback<PopupFeatures, WebEngine>>

Returns:

the createPopupHandler property
createPopupHandlerProperty
()

JavaScript popup handler property.

private static File
pack-priv void
public final ReadOnlyObjectProperty<Document>

Returns:

the document property
documentProperty
()

Document object for the current Web page.

public Object

Returns:

execution result, converted to a Java object using the following rules:
  • JavaScript Int32 is converted to java.lang.Integer
  • Other JavaScript numbers to java.lang.Double
  • JavaScript string to java.lang.String
  • JavaScript boolean to java.lang.Boolean
  • JavaScript null to null
  • Most JavaScript objects get wrapped as netscape.javascript.JSObject
  • JavaScript JSNode objects get mapped to instances of netscape.javascript.JSObject, that also implement org.w3c.dom.Node
  • A special case is the JavaScript class JavaRuntimeObject which is used to wrap a Java object as a JavaScript value - in this case we just extract the original Java value.
executeScript
(String
the script
script
)

Executes a script in the context of the current page.

private void
fireError(EventType<WebErrorEvent> eventType, String message, Throwable exception)

public final Callback<String, Boolean>
public final Callback<PopupFeatures, WebEngine>
pack-priv Debugger
public final Document
public WebHistory

Returns:

history object
getHistory
()

Returns the session history object.

public final Worker<Void>

Returns:

the Worker object
getLoadWorker
()

Returns a javafx.concurrent.Worker object that can be used to track loading progress.

public final String
private long
public final EventHandler<WebEvent<String>>
public final EventHandler<WebErrorEvent>
public final EventHandler<WebEvent<Rectangle2D>>
public final EventHandler<WebEvent<String>>
public final EventHandler<WebEvent<Boolean>>
pack-priv WebPage
public final Callback<PromptData, String>
public final String
public final String
public final File
public final String
public final boolean
public final BooleanProperty
public void
load(String
URL of the web page to load
url
)

Loads a Web page into this engine.

public void
loadContent(String
the HTML content to load
content
)

Loads the given HTML content directly.

public void
loadContent(String
the HTML content to load
content
,
String
the type of content to load
contentType
)

Loads the given content directly.

public final ReadOnlyStringProperty

Returns:

the location property
locationProperty
()

URL of the current Web page.

public final ObjectProperty<EventHandler<WebEvent<String>>>

Returns:

the onAlert property
onAlertProperty
()

JavaScript alert handler property.

public final ObjectProperty<EventHandler<WebErrorEvent>>
public final ObjectProperty<EventHandler<WebEvent<Rectangle2D>>>

Returns:

the onResized property
onResizedProperty
()

JavaScript window resize handler property.

public final ObjectProperty<EventHandler<WebEvent<String>>>

Returns:

the onStatusChanged property
onStatusChangedProperty
()

JavaScript status handler property.

public final ObjectProperty<EventHandler<WebEvent<Boolean>>>

Returns:

the onVisibilityChanged property
onVisibilityChangedProperty
()

JavaScript window visibility handler property.

private static final boolean
public final ObjectProperty<Callback<PromptData, String>>

Returns:

the promptHandler property
promptHandlerProperty
()

JavaScript prompt handler property.

private byte[]
public void
reload()

Reloads the current page, whether loaded from URL or directly from a String in one of the loadContent methods.

public final void
public final void
public final void
setJavaScriptEnabled(boolean value)

public final void
public final void
public final void
public final void
public final void
public final void
public final void
public final void
public final void
pack-priv void
private void
stop()

public final ReadOnlyStringProperty

Returns:

the title property
titleProperty
()

Title of the current Web page.

private void
private void
public final StringProperty
public final ObjectProperty<File>
public final StringProperty
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait