2

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

2
  • Just call the reload() method on your WebEngine on a loop that pauses 10 seconds between each iteration? Commented Jun 20, 2019 at 17:18
  • You can execute a javascript function using: webEngine.executeScript("myFunction()"); where myFunction can be used to refresh the page on javascript side. Check this link Commented Jun 20, 2019 at 17:26

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.