Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a local HTML file using WebView in JavaFX

I am looking for a way to display an html file in a different stage once the help button is clicked.

public void handleButtonAction(ActionEvent event) throws IOException {
        if (event.getSource() == help) {
            stage = (Stage) help.getScene().getWindow();
            root = FXMLLoader.load(getClass().getResource("help.fxml"));
            WebView browser = new WebView();
            Scene helpScene = new Scene(root);
            Stage helpStage = new Stage();
            helpStage.setTitle("Help Menu");
            helpStage.setScene(helpScene);
            URL url = getClass().getResource("readme.html");
            browser.getEngine().load(url.toExternalForm());
            helpStage.show();
       }
}
like image 284
Moe Avatar asked Jul 20 '15 22:07

Moe


People also ask

Can you use HTML in JavaFX?

It supports Cascading Style Sheets (CSS), JavaScript, Document Object Model (DOM), and HTML5. The embedded browser enables you to perform the following tasks in your JavaFX applications: Render HTML content from local and remote URLs. Obtain Web history.

What is JavaFX WebView?

WebView is a Node that manages a WebEngine and displays its content. The associated WebEngine is created automatically at construction time and cannot be changed afterwards. WebView handles mouse and some keyboard events, and manages scrolling automatically, so there's no need to put it into a ScrollPane .

Can you make a website with JavaFX?

Basically, no. You can try to create a JavaFX application and deploy it as an applet, but making it work with browser security restrictions is prohibitively difficult imho.

Does JavaFX use JavaScript?

A JavaFX application can communicate with the web page in which it is embedded by using a JavaScript engine. The host web page can also communicate to embedded JavaFX applications using JavaScript.


1 Answers

Your code is fine except that you forgot to add the webview to the scene, do

((Pane) helpScene.getRoot()).getChildren().add(browser);
like image 135
Uluk Biy Avatar answered Nov 11 '22 11:11

Uluk Biy