JavaFx WebView自动刷新?

时间:2019-06-20 15:06:46

标签: java javafx

我想自动刷新JavaFX WebView。 你能帮我吗? 估计需要每10秒刷新一次

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);
    }

}

我要在大屏幕上镜像,所以我需要刷新。我将它与.reload一起使用,但没有用。每10秒

1 个答案:

答案 0 :(得分:3)

您可以使用Timeline每10秒重新加载页面:

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();