0

I am creating an AppDrawer (all in one place to launch shortcuts).

My JSCallBack bridge class has a method that should delete a shortcut, then refresh the page. But, it is unable to successfully refresh the page.

AppDrawerMain.java

public class AppDrawerMain extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(AppDrawerMain.class.getResource("main.fxml"));
        Parent root = (Parent) fxmlLoader.load();

        Scene scene = new Scene(root, 1280, 720);
        String css = this.getClass().getResource("application.css").toExternalForm();
        scene.getStylesheets().add(css);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

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

AppDrawerController.java

public class AppDrawerController implements Initializable {
    @FXML
    private Button refreshButton;

    @FXML
    private WebView webView;

    WebEngine webEngine;
    JSCallBack jsCallBack;
    
    //this function essentially generates the html code for the webview
    public String loadApps(){
        return "<div class=\"container\"><img src=\"file:/"+imagePath+"\"/><p class=\"title\">"+displayName+"</p><div class=\"overlay\"></div><div class=\"button-open\" onclick=\"app.processOnClickOpen(\'"+id+"\')\"><a href=\"#\"> Open </a></div><div class=\"button-option\" onclick=\"app.processOnClickOption(\'"+id+"\')\"><a href=\"#\"> Edit </a></div></div>"
    }
   
    //refresh the page
    private void refreshPage(String html){
        webEngine.loadContent(html);
        webEngine.reload();
    }

    @SneakyThrows
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        refreshButton.setOnAction(event -> {
            refreshPage(loadApps());
        });
        webEngine = webView.getEngine();
        webEngine.getLoadWorker().stateProperty().addListener((obs, oldValue, newValue)-> {
            if (newValue == Worker.State.SUCCEEDED) {
                JSObject jsObject = (JSObject) webEngine.executeScript("window");
                jsCallBack = new JSCallBack(webEngine); //declared this way to avoid GC
                jsObject.setMember("app", jsCallBack);
            }
        });
        webEngine.setJavaScriptEnabled(true);
        var html = loadApps();
        webEngine.loadContent(html);
    }

    //The bridge class
    public class JSCallBack {
        protected JSCallBack() {}

        //no refresh needed here
        public void processOnClickOpen(String id) {
            log("Before open"); 
            onAppOpen(id);
            log("After open");
        }

        //The part that isnt working
        public void processOnClickOption(String id) {
            //deleting the apps works fine
            webEngine.loadContent(loadApps()+loadApps()); //trying to refresh the page isnt fine
            webEngine.reload();           
        }  
    }
}

The problem I am having is here:

public void processOnClickOption(String id) {          
    //refreshPage(loadApps()+loadApps()) (not working either)
    //refreshButton.fire() (not working either)
    webEngine.loadContent(loadApps()+loadApps()); //trying to refresh the page isnt fine
    webEngine.reload(); //not working   
}
  1. I tried adding location.reload() to the JavaScript function in the script itself, but it did not work.

  2. I tried adding the refreshButton.fire() to the processOnClickOption(), which should reload the page if clicked on manually, but it did work either.

  3. I tried to set a new WebEngine in the bridge class itself. Also, it did not work.

  4. I added a log to see if there was an issue with threading, but WebEngine mentions that it was on the JavaFX Application thread, so that is not the problem either.

This is the main WebView:

webview

If I click on the edit button, it should remove it from the WebView and refresh the page like this expected outcome:

outcome

Unfortunately, after clicking the edit button it will delete the files on the backend side, but the WebView is not refreshed, so the app stays there. I would like to get help on this.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
Izzaz
  • 1
  • 1

0 Answers0