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>
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.