How to display a web page in Java

Crossability Winmostar Support team.

1. Display a web page in Java

JavaFX, Java's default GUI, has the ability to display web pages. In this article, I'll show you how. Web pages can be viewed using a class called WebView. Below is the sample code.

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

public class WebTest extends Application {

    private WebView webView;

    @Override
    public void start(Stage stage) {
        this.webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.load("https://x-ability.co.jp");

        BorderPane pane = new BorderPane(this.webView);
        Scene scene = new Scene(pane, 500, 500);

        stage.setScene(scene);
        stage.setMaximized(true);
        stage.show();
    }

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

2. URL update

Use WebEngine # load to update the URL set in WebView.

    private void updateURL(String url) {
        WebEngine webEngine = this.webView.getEngine();
        webEngine.load(url);
    }

3. Go forward / back on page

Use WebHistory # go to go back or forth on the page you are viewing.

    private void goForward() {
        WebEngine webEngine = this.webView.getEngine();
        WebHistory webHistory = webEngine.getHistory();
        webHistory.go(+1);
    }

    private void goBackward() {
        WebEngine webEngine = this.webView.getEngine();
        WebHistory webHistory = webEngine.getHistory();
        webHistory.go(-1);
    }

4. Close the window

Implements JavaFX window closing process when a window closing event occurs from a web page. Use WebEngine # setOnVisibilityChanged.

    @Override
    public void start(Stage stage) {
        ...abridgement...
        stage.show();

        webEngine.setOnVisibilityChanged(event -> {
            if (!event.getData()) {
                stage.hide();
            }
        });
    }

5. Display a pop-up

Implement the process to display a new window in JavaFX when an event to display a pop-up occurs from a web page. Use WebEngine # setCreatePopupHandler.

    @Override
    public void start(Stage stage) {
        ...abridgement...
        stage.show();

        webEngine.setCreatePopupHandler(config -> {
            WebView webView2 = new WebView();
            WebEngine webEngine2 = webView2.getEngine();
            webEngine2.load("about:blank");

            BorderPane pane2 = new BorderPane(webView2);
            Scene scene2 = new Scene(pane2, 500, 500);

            Stage stage2 = new Stage();
            stage2.setScene(scene2);
            stage2.show();

            return webEngine2;
        });
    }

Recommended Posts

How to display a web page in Java
[Java] How to display Wingdings
How to create a Java environment in just 3 seconds
How to create a data URI (base64) in Java
How to display a browser preview in VS Code
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
How to make a Java container
How to learn JAVA in 7 days
[Java] How to create a folder
How to use classes in Java?
How to name variables in Java
How to make a Java array
How to concatenate strings in java
How to store a string from ArrayList to String in Java (Personal)
How to display a graph in Ruby on Rails (LazyHighChart)
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
How to make a Java calendar Summary
How to implement Kalman filter in Java
How to insert a video in Rails
How to do base conversion in Java
[Introduction to Java] How to write a Java program
How to make a Discord bot (Java)
How to implement coding conventions in Java
How to embed Janus Graph in Java
How to print a Java Word document
How to get the date in java
How to publish a library in jCenter
How to display error messages in Japanese
How to implement a job that uses Java API in JobScheduler
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to automatically operate a screen created in Java on Windows
Two ways to start a thread in Java + @
[Personal memo] How to interact with a random number generator in Java
Code to escape a JSON string in Java
Try to create a bulletin board in Java
How to get Class from Element in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
Let's create a super-simple web framework in Java
How to convert a solidity contract to a Java contract class
How to run a djUnit task in Ant
How to add a classpath in Spring Boot
How to create a theme in Liferay 7 / DXP
How to hide null fields in response in Java
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to pass a proxy when throwing REST over SSL in Java
How to get the absolute path of a directory running in Java
[Java] How to substitute Model Mapper in Jackson
How to solve an Expression Problem in Java
How to migrate a web application created in a local docker environment to AWS
How to make a follow function in Rails
[Java] [For beginners] How to insert elements directly in a 2D array
How to write Java String # getBytes in Kotlin?
How to automatically generate a constructor in Eclipse
How to ZIP a JAVA CSV file and manage it in a Byte array
How to clear all data in a particular table
[Java] How to omit the private constructor in Lombok
How to implement a like feature in Ajax in Rails
How to jump from Eclipse Java to a SQL file