I am curious as to the differences between the following approaches to creating a Swing window:
- Using java.awt.EventQueue.invokeLater() in main();
- Using SwingUtilities.invokeLater() in main();
- not bothering with threads at all, simply instantiating the JFrame subclass and calling setVisible(true) from main without wrapping it inside a Runnable; this seems to work anyway.
Thanks.
The thing to bear in mind with threading is that “seems to work” isn´t the same as “will demonstrably work under all circumstances”.
The basic rule is you shouldn´t create Swing/manipulate components outside the event thread, and application’s main thread is “outside the event thread”. So in your application startup code, you should create your main window in an invokeLater().
If you’re programming with Swing, I would use the SwingUtilities version of invokeLater(). Even though I think functionally in current implementations one just calls the other, I guess this could change in the future.