How to pass data from JavaScript to JavaFX

2019-02-27 18:03发布

问题:

In my JavaFX application, I have a WebView to load HTML contents. Is there a way to pass Javascript variables and manipulate it in JavaFX App. Let's say I have a variable FULLNAME in Javascript and I want to display it in a TextField in JavaFX. Is it possible? Can you give an example.

回答1:

It is possible - assuming your webview is in a webView variable, and you want your FULLNAME variable loaded into fullNameField, try executing this on the Java side:

String fullName = (String) webview1.getEngine().executeScript("FULLNAME;");
fullNameField.setText(fullName);