Java - get return value of executeScript within thread
I'm trying to execute javascript on a WebView and get the output. This works but I'm getting an error when trying to execute this within a thread:
@FXML
private void handleButtonAction(ActionEvent event) throws InterruptedException {
int x = this.test();
System.out.print(x);
}
private int test()
{
return (Integer) engine.executeScript("function x(){return 3}; x();");
}
Gives output '3' and doesn't throw an exception, yet when I execute:
public void exampleThread() {
Thread one = new Thread() {
public void run() {
while (true) {
try {
System.out.println("Does it work?");
Thread.sleep(1000);
int y = test();
System.out.println("Nope: "+y);
} catch (InterruptedException v) {
System.out.println(v);
}
}
}
};
one.start();
}
It throws:
Exception in thread "Thread-12" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-12
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:279)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
at javafx.scene.web.WebEngine.checkThread(WebEngine.java:1243)
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:1003)
at testApp.FXMLDocumentController.test(FXMLDocumentController.java:91)
at testApp.FXMLDocumentController.access$000(FXMLDocumentController.java:28)
at testApp.FXMLDocumentController$1.run(FXMLDocumentController.java:76)
Does anybody know what's going on here?
java multithreading javafx webview
add a comment |
I'm trying to execute javascript on a WebView and get the output. This works but I'm getting an error when trying to execute this within a thread:
@FXML
private void handleButtonAction(ActionEvent event) throws InterruptedException {
int x = this.test();
System.out.print(x);
}
private int test()
{
return (Integer) engine.executeScript("function x(){return 3}; x();");
}
Gives output '3' and doesn't throw an exception, yet when I execute:
public void exampleThread() {
Thread one = new Thread() {
public void run() {
while (true) {
try {
System.out.println("Does it work?");
Thread.sleep(1000);
int y = test();
System.out.println("Nope: "+y);
} catch (InterruptedException v) {
System.out.println(v);
}
}
}
};
one.start();
}
It throws:
Exception in thread "Thread-12" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-12
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:279)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
at javafx.scene.web.WebEngine.checkThread(WebEngine.java:1243)
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:1003)
at testApp.FXMLDocumentController.test(FXMLDocumentController.java:91)
at testApp.FXMLDocumentController.access$000(FXMLDocumentController.java:28)
at testApp.FXMLDocumentController$1.run(FXMLDocumentController.java:76)
Does anybody know what's going on here?
java multithreading javafx webview
You can not update the UI on a background thread. I suspect yourtest()
method and the JavaScript within it does something to update the UI (isengine
yourWebView
?).
– Zephyr
Nov 19 '18 at 17:41
I've read about that but strangely enough not. All it does is output it to the console, nowhere does it alter anything of the UI. The code above is pretty much as is.
– Mihael Keehl
Nov 19 '18 at 17:52
add a comment |
I'm trying to execute javascript on a WebView and get the output. This works but I'm getting an error when trying to execute this within a thread:
@FXML
private void handleButtonAction(ActionEvent event) throws InterruptedException {
int x = this.test();
System.out.print(x);
}
private int test()
{
return (Integer) engine.executeScript("function x(){return 3}; x();");
}
Gives output '3' and doesn't throw an exception, yet when I execute:
public void exampleThread() {
Thread one = new Thread() {
public void run() {
while (true) {
try {
System.out.println("Does it work?");
Thread.sleep(1000);
int y = test();
System.out.println("Nope: "+y);
} catch (InterruptedException v) {
System.out.println(v);
}
}
}
};
one.start();
}
It throws:
Exception in thread "Thread-12" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-12
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:279)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
at javafx.scene.web.WebEngine.checkThread(WebEngine.java:1243)
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:1003)
at testApp.FXMLDocumentController.test(FXMLDocumentController.java:91)
at testApp.FXMLDocumentController.access$000(FXMLDocumentController.java:28)
at testApp.FXMLDocumentController$1.run(FXMLDocumentController.java:76)
Does anybody know what's going on here?
java multithreading javafx webview
I'm trying to execute javascript on a WebView and get the output. This works but I'm getting an error when trying to execute this within a thread:
@FXML
private void handleButtonAction(ActionEvent event) throws InterruptedException {
int x = this.test();
System.out.print(x);
}
private int test()
{
return (Integer) engine.executeScript("function x(){return 3}; x();");
}
Gives output '3' and doesn't throw an exception, yet when I execute:
public void exampleThread() {
Thread one = new Thread() {
public void run() {
while (true) {
try {
System.out.println("Does it work?");
Thread.sleep(1000);
int y = test();
System.out.println("Nope: "+y);
} catch (InterruptedException v) {
System.out.println(v);
}
}
}
};
one.start();
}
It throws:
Exception in thread "Thread-12" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-12
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:279)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
at javafx.scene.web.WebEngine.checkThread(WebEngine.java:1243)
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:1003)
at testApp.FXMLDocumentController.test(FXMLDocumentController.java:91)
at testApp.FXMLDocumentController.access$000(FXMLDocumentController.java:28)
at testApp.FXMLDocumentController$1.run(FXMLDocumentController.java:76)
Does anybody know what's going on here?
java multithreading javafx webview
java multithreading javafx webview
asked Nov 19 '18 at 17:32
Mihael KeehlMihael Keehl
19011
19011
You can not update the UI on a background thread. I suspect yourtest()
method and the JavaScript within it does something to update the UI (isengine
yourWebView
?).
– Zephyr
Nov 19 '18 at 17:41
I've read about that but strangely enough not. All it does is output it to the console, nowhere does it alter anything of the UI. The code above is pretty much as is.
– Mihael Keehl
Nov 19 '18 at 17:52
add a comment |
You can not update the UI on a background thread. I suspect yourtest()
method and the JavaScript within it does something to update the UI (isengine
yourWebView
?).
– Zephyr
Nov 19 '18 at 17:41
I've read about that but strangely enough not. All it does is output it to the console, nowhere does it alter anything of the UI. The code above is pretty much as is.
– Mihael Keehl
Nov 19 '18 at 17:52
You can not update the UI on a background thread. I suspect your
test()
method and the JavaScript within it does something to update the UI (is engine
your WebView
?).– Zephyr
Nov 19 '18 at 17:41
You can not update the UI on a background thread. I suspect your
test()
method and the JavaScript within it does something to update the UI (is engine
your WebView
?).– Zephyr
Nov 19 '18 at 17:41
I've read about that but strangely enough not. All it does is output it to the console, nowhere does it alter anything of the UI. The code above is pretty much as is.
– Mihael Keehl
Nov 19 '18 at 17:52
I've read about that but strangely enough not. All it does is output it to the console, nowhere does it alter anything of the UI. The code above is pretty much as is.
– Mihael Keehl
Nov 19 '18 at 17:52
add a comment |
1 Answer
1
active
oldest
votes
You are using the WebEngine that is "inside" JavaFX - and it seems to want to run in the JavaFX application thread, which is where you are when executing your event handler in handleButtonAction()
- but not when you run your own thread. The simplest solution is just to run your test method in the JavaFX thread (look up Platform.invokeLater()
?), or you could probably just hand the script to the JavaScript engine in Java, not the one in JavaFX (which would free you from having to run in the JavaFX application thread...)
3
It's Platform.runLater
– Gnas
Nov 19 '18 at 17:54
Thank you for answer, unfortunately I don't quite understand it. When I run it within the JavaFX function directly I still receive the same error or is that not what you mean?
– Mihael Keehl
Nov 19 '18 at 18:01
I thought you said that your first example (which is the one that runs within the JavaFX handler?) does work ok?
– moilejter
Nov 19 '18 at 18:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379878%2fjava-get-return-value-of-executescript-within-thread%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are using the WebEngine that is "inside" JavaFX - and it seems to want to run in the JavaFX application thread, which is where you are when executing your event handler in handleButtonAction()
- but not when you run your own thread. The simplest solution is just to run your test method in the JavaFX thread (look up Platform.invokeLater()
?), or you could probably just hand the script to the JavaScript engine in Java, not the one in JavaFX (which would free you from having to run in the JavaFX application thread...)
3
It's Platform.runLater
– Gnas
Nov 19 '18 at 17:54
Thank you for answer, unfortunately I don't quite understand it. When I run it within the JavaFX function directly I still receive the same error or is that not what you mean?
– Mihael Keehl
Nov 19 '18 at 18:01
I thought you said that your first example (which is the one that runs within the JavaFX handler?) does work ok?
– moilejter
Nov 19 '18 at 18:24
add a comment |
You are using the WebEngine that is "inside" JavaFX - and it seems to want to run in the JavaFX application thread, which is where you are when executing your event handler in handleButtonAction()
- but not when you run your own thread. The simplest solution is just to run your test method in the JavaFX thread (look up Platform.invokeLater()
?), or you could probably just hand the script to the JavaScript engine in Java, not the one in JavaFX (which would free you from having to run in the JavaFX application thread...)
3
It's Platform.runLater
– Gnas
Nov 19 '18 at 17:54
Thank you for answer, unfortunately I don't quite understand it. When I run it within the JavaFX function directly I still receive the same error or is that not what you mean?
– Mihael Keehl
Nov 19 '18 at 18:01
I thought you said that your first example (which is the one that runs within the JavaFX handler?) does work ok?
– moilejter
Nov 19 '18 at 18:24
add a comment |
You are using the WebEngine that is "inside" JavaFX - and it seems to want to run in the JavaFX application thread, which is where you are when executing your event handler in handleButtonAction()
- but not when you run your own thread. The simplest solution is just to run your test method in the JavaFX thread (look up Platform.invokeLater()
?), or you could probably just hand the script to the JavaScript engine in Java, not the one in JavaFX (which would free you from having to run in the JavaFX application thread...)
You are using the WebEngine that is "inside" JavaFX - and it seems to want to run in the JavaFX application thread, which is where you are when executing your event handler in handleButtonAction()
- but not when you run your own thread. The simplest solution is just to run your test method in the JavaFX thread (look up Platform.invokeLater()
?), or you could probably just hand the script to the JavaScript engine in Java, not the one in JavaFX (which would free you from having to run in the JavaFX application thread...)
answered Nov 19 '18 at 17:42
moilejtermoilejter
62447
62447
3
It's Platform.runLater
– Gnas
Nov 19 '18 at 17:54
Thank you for answer, unfortunately I don't quite understand it. When I run it within the JavaFX function directly I still receive the same error or is that not what you mean?
– Mihael Keehl
Nov 19 '18 at 18:01
I thought you said that your first example (which is the one that runs within the JavaFX handler?) does work ok?
– moilejter
Nov 19 '18 at 18:24
add a comment |
3
It's Platform.runLater
– Gnas
Nov 19 '18 at 17:54
Thank you for answer, unfortunately I don't quite understand it. When I run it within the JavaFX function directly I still receive the same error or is that not what you mean?
– Mihael Keehl
Nov 19 '18 at 18:01
I thought you said that your first example (which is the one that runs within the JavaFX handler?) does work ok?
– moilejter
Nov 19 '18 at 18:24
3
3
It's Platform.runLater
– Gnas
Nov 19 '18 at 17:54
It's Platform.runLater
– Gnas
Nov 19 '18 at 17:54
Thank you for answer, unfortunately I don't quite understand it. When I run it within the JavaFX function directly I still receive the same error or is that not what you mean?
– Mihael Keehl
Nov 19 '18 at 18:01
Thank you for answer, unfortunately I don't quite understand it. When I run it within the JavaFX function directly I still receive the same error or is that not what you mean?
– Mihael Keehl
Nov 19 '18 at 18:01
I thought you said that your first example (which is the one that runs within the JavaFX handler?) does work ok?
– moilejter
Nov 19 '18 at 18:24
I thought you said that your first example (which is the one that runs within the JavaFX handler?) does work ok?
– moilejter
Nov 19 '18 at 18:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379878%2fjava-get-return-value-of-executescript-within-thread%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can not update the UI on a background thread. I suspect your
test()
method and the JavaScript within it does something to update the UI (isengine
yourWebView
?).– Zephyr
Nov 19 '18 at 17:41
I've read about that but strangely enough not. All it does is output it to the console, nowhere does it alter anything of the UI. The code above is pretty much as is.
– Mihael Keehl
Nov 19 '18 at 17:52