Channa
Channa

Reputation: 21

How to reload the WebView in javafx

I have written code to render my html page( html page is from my local machine) in JavaFX application and now I would like to reload webview whenever the html page(specified) gets modified. And html modification is done by other application.

Can anyone please let me know how to reload the webview . Here's the my code :

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

    }
}

Thanks in advance.

Upvotes: 1

Views: 2340

Answers (2)

Channa
Channa

Reputation: 21

I've found an answer using below code:

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

Upvotes: 0

Andrew Butenko
Andrew Butenko

Reputation: 395

What about checking for changes periodically?

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

}
}

Upvotes: 1

Related Questions