JavaFX: First Steps - Correcting a Swing Mistake
by Tim O'Brien
Those of you familiar with Swing may have noticed an error in the last post. This is a correction to First Steps "Hello OnJava".
On the JavaFX users mailing list Dean Iverson pointed out that the intermittent exceptions at startup where most likely due to the fact that I wasn't using SwingUtilities.invokeLater(). Here's the text of his reply:
When I see intermittent exceptions at startup, the first thing I think of is manipulating components off of the EDT. I see from your code that you are creating your frame, running your script, and setting the frame visible from the app's main thread. The first thing I would try is to wrap that stuff in a Runnable and use SwingUtils.invokeLater.
Right, right, it's been so long since I've picked up Swing I forgot about this. But, this makes perfect sense, part of the promise of JavaFX is that it is going to convince programmers like myself to reconsider Java GUI programming. Hopefully following the series will help others in the same situation.
Read more to see the modified Main.java from the previous post...
|InvokeLtare Causes doRun.run() to be executed asynchronously ON the AWT event dispatching thread, not "off of it"|
|rereading the quote its ambig - maybe you meant that the new version does it on the thread (which it should). It is one of my concerns about JavaFX, as the whole advantage of Java is multithreading (so you can asychronously talk to servers) - so unless you can do this in JavaFX it will be a step backwards.|
|Nope - your correction in the last article is definitely a 'feature' "I was trying to execute the FX script in the EDT.". Nope, you were trying to execute it outside the EDT at first, all changes of state to the GUI (except initial creation) have to be done ON the EDT.|