[NOTE: Every time I have a question with JavaFX, I think I will post it here before searching elsewhere. If/when I find an answer, I will follow up and include it here, assuming no one beats me to it. Goal: JGO forums become more relevant to Java game programmers using JavaFX if we have more questions and tutorials at this site.]
I ran into the following error when trying to add or delete a Node from a method that is NOT the overridden start(Stage) method.
java.lang.IllegalStateException: Not on FX application thread; ...
Example of calling code that elicits this error:
root.getChildren().add(img);
For nodes added in the start() method, the code: root.getChildren().remove(img); creates the same error.
The code that tries to add or remove is located in a method in the same class that implements Application . A Group variable named root has been made an instance variable rather than the usual practice of making the root node a local variable of the start() method.
My goal is to manage cross-fades between different visual elements. I was thinking that once a node is faded out and no longer needed, I would delete it. And as new nodes are needed, I would add them. But the Exception is preventing this.
Alternatively, I can access the existing nodes to null them or vary their properties. Is this the way to manage the nodes? Flip the visibility or manage opacity and null them when they are not being used, but leave the nulled nodes as placeholders? That could leave me with a scene with 80% of the nodes being null or invisible at any one time which intuitively seems kind of fishy.
Or is this sort of thing (cross-fading lots of nodes) something that can or should be handled with managing multiple Scene variables?