0

I'm working on an application that reads a method (println()) from the Java code in the JavaScript code in a local HTML file that is loaded by WebEngine when the user selects an item in ComboBox.

The method is successfully called the first time the user selects an item in ComboBox (which is the first time the content is loaded in WebEngine), but after the first time, the method is no longer executed.

If you can explain to me how to write the code in a way that the method is executed even after the first time the content is loaded in WebEngine.

My Controller.java:

public class Controller {

    ObservableList<String> months = FXCollections.observableArrayList
        ("Tishrei", "Cheshvan", "Kislev", "Tevet", "Shevat", "Adar",
        "Nisan", "Iyar", "Sivan", "Tammuz", "Av", "Elul");

    public ComboBox<String> month;
    public WebView webView;

    public void println(String s){
        System.out.println(s);
    }

    @FXML
    public void initialize() {
        month.setItems(months);

        WebEngine webEngine = webView.getEngine();
        JSObject win = (JSObject) webEngine.executeScript("window");
        win.setMember("app", this);

        month.setOnAction(event -> {
            String loadFile = this.getClass().getResource("index.html").toString();
            webEngine.load(loadFile);
        });
    }
}

The script in HTML file:

<html>
    <head>
        <meta charset="UTF-8">
    </head>

    <body>
        <script type="text/javascript">
            app.println("Hallow World!");
        </script>
    </body>
</html>
9
  • Can you elaborate please? Commented May 7, 2020 at 3:59
  • Right. But methods can be imported from Java code to JavaScript code as in the example above by executeScript (see about it here) and the method is done, but my problem is that it only happens the first time the content is loaded in WebEngine. Commented May 7, 2020 at 4:10
  • So maybe you can somehow get the JavaScript code to reload on the server every time the user selects an item in ComboBox? Commented May 7, 2020 at 4:21
  • This is just about uploading a local file. Commented May 7, 2020 at 4:28
  • So does that mean it can't be done, or is it possible but not recommended? Commented May 7, 2020 at 4:41

0

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.