Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javafx中重新加载WebView_Java_Javafx - Fatal编程技术网

如何在javafx中重新加载WebView

如何在javafx中重新加载WebView,java,javafx,Java,Javafx,我已经编写了代码来在JavaFX应用程序中呈现我的html页面(html页面来自我的本地机器),现在我想在修改html页面(指定)时重新加载webview。 html修改由其他应用程序完成 有人能告诉我如何重新加载webview吗。 以下是我的代码: package view; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOExceptio

我已经编写了代码来在JavaFX应用程序中呈现我的html页面(html页面来自我的本地机器),现在我想在修改html页面(指定)时重新加载webview。 html修改由其他应用程序完成

有人能告诉我如何重新加载webview吗。 以下是我的代码:

package view;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.Timer;
import java.util.TimerTask;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class ProgressView extends Application {

    private Scene scene;
    Browser br = new Browser("E:\\Developer-Job\\test.html");
    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Web View");

        scene = new Scene(br,750,500, Color.web("#666970"));
        stage.setScene(scene);      

        //scene.getStylesheets().add("webviewsample/BrowserToolbar.css");        
        stage.show();




    }
    class Browser extends Region {

        final WebView browser = new WebView();
        final WebEngine webEngine = browser.getEngine();

        public Browser(String url) {
            //apply the styles
            getStyleClass().add("browser");
            // load the web page 
            String strXml = "";
            String strBuilt ="";
            try {
                File f = new File(url);
                webEngine.load(f.toURI().toURL().toString());
                //add the web view to the scene
                getChildren().add(browser);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



        }
        private Node createSpacer() {
            Region spacer = new Region();
            HBox.setHgrow(spacer, Priority.ALWAYS);
            return spacer;
        }

        @Override protected void layoutChildren() {
            double w = getWidth();
            double h = getHeight();
            layoutInArea(browser,0,0,w,h,0, HPos.CENTER, VPos.CENTER);
        }

        @Override protected double computePrefWidth(double height) {
            return 750;
        }

        @Override protected double computePrefHeight(double width) {
            return 500;
        }

    }
}

提前感谢。

定期检查更改如何

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.File;
import java.io.IOException;

public class ProgressView extends Application {


@Override
public void start(Stage stage) throws Exception {
    stage.setTitle("Web View");
    Browser br = new Browser("E:\\Developer-Job\\test.html");

    Scene scene = new Scene(br, 750, 500, Color.web("#666970"));
    stage.setScene(scene);

    //scene.getStylesheets().add("webviewsample/BrowserToolbar.css");
    stage.show();


}

class Browser extends Region {

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    public Browser(String url) {
        getChildren().add(browser);
        //apply the styles
        getStyleClass().add("browser");
        // load the web page
        String strXml = "";
        String strBuilt = "";
        load(url);
        Timeline timeline = new Timeline(new KeyFrame(
                Duration.millis(2500),
                ae -> load(url)));
        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.play();
    }

    private void load(String url) {
        try {
            File f = new File(url);
            webEngine.load(f.toURI().toURL().toString());
            //add the web view to the scene
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private Node createSpacer() {
        Region spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);
        return spacer;
    }

    @Override
    protected void layoutChildren() {
        double w = getWidth();
        double h = getHeight();
        layoutInArea(browser, 0, 0, w, h, 0, HPos.CENTER, VPos.CENTER);
    }

    @Override
    protected double computePrefWidth(double height) {
        return 750;
    }

    @Override
    protected double computePrefHeight(double width) {
        return 500;
    }

}
}

我使用以下代码找到了答案:

webEngine.documentProperty().addListener(new ChangeListener<Document>() {
    @Override 
    public void changed(ObservableValue<? extends Document> observableValue, Document document, Document newDoc) {
        if (newDoc != null) {
            //webEngine.documentProperty().removeListener(this);
            try {
                webEngine.load(f.toURI().toURL().toString());
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
});
webEngine.documentProperty().addListener(新的ChangeListener()){
@凌驾

public void changed(公共无效)已更改。谢谢Andrew的解决方案。