Potential (undesirable) answer
You could invoke node.getScene() safely using Platform.runLater(), and return the result as defined in: Return result from javafx platform runlater and it would work, but I don't think I'd recommend that on a very frequent basis as it could be quite an inefficient approach for a concurrent application.
Potential alternate approach
What follows is more an alternate suggestion than an answer and there is a reasonable chance it will not fit your requirements, but is probably worth an answer for consideration.
I advise flipping your logic around. Instead of:
if given node belongs to a scene. And if not operations could be performed immediately in other case - with runLater method.
Try:
- if you are on the JavaFX application thread perform the operation immediately, otherwise, just assume the item is attached to an active scene and perform the operation later via runLater
This is how things in other concurrent items internal to JavaFX such as tasks work. An example from the Task implementation is:
// If this method was called on the FX application thread, then we can
// just update the state directly and this will make sure that after
// the cancel method was called, the state will be set correctly
// (otherwise it would be indeterminate). However if the cancel method was
// called off the FX app thread, then we must use runLater, and the
// state flag will not be readable immediately after this call. However,
// that would be the case anyway since these properties are not thread-safe.
if (isFxApplicationThread()) {
setState(State.CANCELLED);
} else {
runLater(() -> setState(State.CANCELLED));
}
. . .
// This method exists for the sake of testing, so I can subclass and override
// this method in the test and not actually use Platform.isFxApplicationThread.
boolean isFxApplicationThread() {
return Platform.isFxApplicationThread();
}