• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you discovered the Oracle tutorials? This is the relevant trail: Concurrency in Swing
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()
 
I've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic