• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can someone explain what is an event-dispatching thread for GUIs & why use it over main thread?

 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

I'm currently learning about Swing but I can't get my head round this piece of the code.
Here is a simplified gui (not interested in the gui part but the execution)

I get the above, create a new instance of SwingDemo in the main thread which starts up the gui through the constructor.
However, then the tutorial says that I should avoid doing the above but do this instead:

Reading, it talks about an event-dispatching thread which has completely lost me... Can someone explain why not just instantiate the object directly instead of creating another thread?

Thanks!
 
Darryl Burke
Bartender
Posts: 5152
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you discovered the Oracle tutorials? This is the relevant trail: Concurrency in Swing
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Scabbia wrote: . . . . . .
That is exactly how you should start off a GUI app (though you will find slightly different versions in different sources). In Java8 you can dispense with the anonymous class, writing something like thisThe compiler knows that invokeLater only takes a Runnable and Runnable has only one method and that method takes no arguments, so you can reduce that part to (). Then you write the contents of that method: new SwingDemo() and separate that from the () by the arrow operator. If you explore the folder you find the SwingDemo$1.class file is not created by that technique.
 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Have you discovered the Oracle tutorials? This is the relevant trail: Concurrency in Swing


Thanks it's starting to make sense . I will definitely start using Oracle tutorials more!
 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: That is exactly how you should start off a GUI app (though you will find slightly different versions in different sources). In Java8 you can dispense with the anonymous class, writing something like thisThe compiler knows that invokeLater only takes a Runnable and Runnable has only one method and that method takes no arguments, so you can reduce that part to (). Then you write the contents of that method: new SwingDemo() and separate that from the () by the arrow operator. If you explore the folder you find the SwingDemo$1.class file is not created by that technique.


That's very interesting, does this also help performance then? (for larger programs).
Is this a lamda expression? The syntax looks very similar
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Scabbia wrote: . . .
That's very interesting, does this also help performance then? (for larger programs).
No. The performance should be the same in both cases.

But if you allow 0.5 seconds to click a button and 0.01μs to run the actionPerformed method, are you going to notice the difference?

Is this a lamda expression? . . .
Yes. Or more precisely this is a lambda expression:-
() -> new SwingDemo()
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!