Re: why sun recommends this way

From: Adam (NOTFORSPAM.a_szczeblewski_at_poczta.onet.pl)
Date: 08/24/04


Date: Tue, 24 Aug 2004 08:32:11 +0200


"Madhur Ahuja" <ef@df.com> wrote in message
news:2ov7edFeps6mU2@uni-berlin.de...
>
> Hello
>
> The final bit of code in HelloWorldSwing--and in all of our
examples--looks
> like this:
> javax.swing.SwingUtilities.invokeLater(new Runnable() {
> public void run() {
> /* create and show the GUI */
> }
> });
>
> You can copy this code and use it as-is. It might look daunting, but
we
> recommend it because it ensures that the GUI won't have a
thread-safety
> problem that could break the UI before it even appears onscreen.
>
> Can anybody explain me, why this is the preferred method of creating
GUI
> program.

Usually you create GUI in the main thread. On the way the
EventDispatchThread
can be started (when you show() the first of your frames) and you have
two threads working on the same resource - GUI. Swing is not thread
safe,
assumes accessing it's objects from within one thread.
The code presented allows moving the GUI-creation from the main thread
to the EDT - so only one thread works with GUI. So the app is safe
again.

HTH,
Adam



Relevant Pages

  • why sun recommends this way
    ... public void run{ ... It might look daunting, but we ... why this is the preferred method of creating GUI ... Winners dont do different things, ...
    (comp.lang.java.gui)
  • Cross-thread GUI call
    ... Is it safe to call a GUI control's members directly from another thread. ... if I launch a background thread when the user clicks a button on my ...
    (microsoft.public.dotnet.languages.csharp)
  • Multithreaded GUI issues
    ... In each of these cases, where the events get passed on to GUI components, ... methods are callable from separate threads and which ones aren't? ... For example, some of these threads might download images and in those cases, ... Is there a list somewhere of what's safe and what's not safe to call from a ...
    (microsoft.public.dotnet.languages.csharp)
  • tkinter: call root.after from a thread?
    ... Is a safe to call the method root.afterfrom a separate thread? ... (The registered callback updates the GUI). ... avoid calling methods such as pack, etc. from a thread, but I was ...
    (comp.lang.python)
  • tkinter and threads
    ... In Tkinter, is it safe to call widget.after(time, callback) from another thread (other than the gui thread)? ...
    (comp.lang.python)