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.
Asked
Active
Viewed 1,206 times
1

Ethyl Casin
- 791
- 2
- 16
- 34
1 Answers
2
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);

Evan Knowles
- 7,426
- 2
- 37
- 71
-
Wow! Just that simple? – Ethyl Casin May 22 '15 at 06:44
-
Yep - check here for more: https://blogs.oracle.com/javafx/entry/communicating_between_javascript_and_javafx – Evan Knowles May 22 '15 at 06:44
-
3The code should be run after the webengine is completed loading the html. Namely when the state is SUCCESSED. – Uluk Biy May 22 '15 at 06:47
-
@Evan Knowles, Sir, is it possible to add a changelistener so that if the javascript variable will change the javafx variable also changes. I mean if FULLNAME will change , the Javafx TextField will also change? Is it possible? – Ethyl Casin May 22 '15 at 08:41
-
@EthylCasin I don't think you can add a `changeListener`, but you can call back into Java from JavaScript whenever you update `FULLNAME`. See http://stackoverflow.com/a/30390913/2137269 – Evan Knowles May 22 '15 at 08:53
-
1@UlukBiy: `State.SUCCEEDED` – Eric Duminil Mar 12 '18 at 13:41