JavaFx WebView auto refresh?

Viewed 740

I want to do an auto refresh of JavaFX WebView. Can you help me with that? Estimated needs to be refreshed every 10 seconds

package ui;

import java.io.IOException;        

import host.*;

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

public class MainUİ extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        StackPane root = new StackPane();
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        //get read .txt file and connect hostinfo 
        GetHost host = new GetHost();
        String hostinfo = host.getHost();
        webEngine.load(hostinfo);
        root.getChildren().add(webView);
        Scene scene = new Scene(root);
        primaryStage.setTitle("Evren-Software");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

I'm going to make a mirror on the big screen, so I need to refresh. I used it with .reload but it didn't work. every 10 seconds

1 Answers

You can use a Timeline to reload the page every 10 seconds:

WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();

KeyFrame keyFrame = new KeyFrame(Duration.seconds(10), event -> {
    webEngine.reload();
    event.consume();
});
Timeline timeline = new Timeline(keyFrame);
timeline.setCycleCount(Animation.INDEFINITE);

GetHost host = new GetHost();
String hostinfo = host.getHost();
webEngine.load(hostinfo);
timeline.play();
Related