Reading the last paragraph at Oracle site.
Why does not the initial thread simply create the GUI itself? Because almost all code that creates or interacts with Swing components must run on the event dispatch thread.
So main()
should not create a Swing JFrame within the initial thread, but should use SwingUtilities.invokeLater()
, up to Java 7 at least. This is also explained in this Q/A).
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
While this is very logical, we must observe that this is nearly never done in tutorials.
I read elsewhere that modern compilers will insert the invokeLater()
call themselves without telling the programmer. Is that true, for exemple when using Eclipse?
Is there some reference to confirm this behavior? Are there different cases to consider?