1

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?

Community
  • 1
  • 1
mins
  • 6,478
  • 12
  • 56
  • 75
  • 3
    No, it is *GUI building tools* which will generate such code. The compiler wouldn't dare: it would make it non-compliant with the JLS. – Marko Topolnik Sep 21 '14 at 13:23
  • It's recomended by Oracle, but usually you'll get no problems when GUI is started from the main thread. It's only to be sure that you're on the right side of force ;) – Sergiy Medvynskyy Sep 21 '14 at 13:46
  • @SergiyMedvynskyy: no _apparent_ problem, as suggested [here](http://stackoverflow.com/a/7158505/230513). – trashgod Sep 21 '14 at 15:47
  • Possible [duplicate](http://stackoverflow.com/q/7156949/230513); possible [duplicate](http://stackoverflow.com/q/19167154/230513). – trashgod Sep 21 '14 at 15:49
  • @trashgod: I agree that the discussion is going to be around "is it good practice to use EDT for GUI initialization?", however my question was about the IDE inserting or not code automatically in `main()` binary to use the EDT. It has not been answered, but from the comments, I guess that's not the case. So no need to continue in a wrong direction. And I voted ot close the question too. – mins Sep 21 '14 at 16:13
  • As @MarkoTopolnik notes, a GUI editor typically _does_ generate a call to `EventQueue.invokeLater()`, but I don't let it [handle the frame](http://stackoverflow.com/a/2561540/230513). A Java 8 lambda expression makes the invocation somewhat less verbose, for [example](http://stackoverflow.com/a/25736893/230513). – trashgod Sep 21 '14 at 16:44

1 Answers1

1

I think the examples don't use invokeLater() because they don't want to complicate the example (and they don't want to explain invokeLater(), at least at that point).

The issue of using the event dispatch thread only applies if there is already a UI running; mostly these examples are creating a UI from scratch, therefore it isn't running when the Swing stuff is called, and until something is realized to the screen, then there are no user-generated events to worry about. One could still get hosed up, I suppose, but they would almost have to try to do so.

So I guess the creation of the UI from scratch is its own special case.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • I agree with your note on why the tutorials don't show this, but the GUI building code probably works just by accident, because it typically doesn't get JIT-optimized. Otherwise you can expect at least some memory visibility issue. – Marko Topolnik Sep 21 '14 at 18:10