JavaFX Load Local HTML File Using WebView

JavaFX is platform to create GUI for desktop application in Java, it has more modern and performance than any other framework for desktop application development. It also support template using FXML and styling using CSS.

You can load an HTML file into JavaFX application using WebView, either it's available online, internet connection required or a local HTML resource, support CSS and Javascript. To load an HTML we can use WebView for example:

1. First create an html and put it on /src/main/resources/helloWorld.html

<html lang="en">
<head><title>Hello World</title></head>
<body><h1>Hello world!</h1></body>
</html>

2. Now for just demo, create a class name HelloWorldApplication.java that contains main method.

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

public class HelloWorldApplication extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.load( getClass().getResource("/helloWorld.html").toString() );
        Scene scene = new Scene(webView,600,600);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Hello World");
        primaryStage.show();
    }

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


Now you can run the main method using your favorite IDE, and will show the content of helloWorld.html as below.

Hello World JavaFX

Popular posts from this blog

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Create Spring Boot Project Using Netbeans

Spring Kafka - How to use ReplyingKafkaTemplate send and reply synchronously