Working with web content
The WebView
component is a component that can render modern HTML pages. It's based on WebKit (https://webkit.org/), an open-source browser engine that is widely used.
WebView consists of two parts:
WebView
itself, which is a JavaFX node and can be used in SceneGraph- The
WebEngine
class, which is responsible for all HTML and JavaScript logic
Note
Technically, there is also a third component—a rendering engine. It's not provided by WebKit and JavaFX has its own. But developers usually do not communicate with it.
Presenting web content with WebView
Let's look at a simpleWebView
example:
// chapter9/web/WebViewDemo.java public void start(Stage primaryStage) { WebView wv = new WebView(); wv.getEngine().load("https://stackoverflow.com/questions/tagged/javafx"); StackPane root = new StackPane(wv); primaryStage.setTitle("JavaFX on SO"); primaryStage.setScene(new Scene(root, 400, 250)); primaryStage.show(); }
This loads and shows a page from stackoverflow.com...