I have some code which adds a Java FX WebView to a JFrame. This works great. Once the user has visited a specific website, I’m trying to programatically close this JFrame, first by calling Platform.exit() and then frame.dispose(). This works fine as long as I call this method from the same thread that the WebView was started from.
However, this is what I am trying to avoid. Consider the following code (http://pastebin.com/kAjweLi5).
This code tries to shut down the program when the URL changes. However, this results in:
IllegalStateException: Attempt to call defer when toolkit not running
(stacktrace)
Update:
Using my (very limited) debuggning knowledge, I realized that the exit() method seems to successfully run. Image of the stack:

(the error occures when the yellow line is executed)
How can I close the JFrame from another thread/class, without getting the above exception?
From previously (amended) post:
It’s necessary to shutdown background threads