Depending on the requirements of your application, you don't necessarily have to 
freeze the whole GUI.

1. Create a thread to do the lengthy work.
2. Disable any portion of the GUI which should be unavailable until the heavy 
processing is finished.  For example, disabling a button or a group of actions, etc.
3. return immediately.
4. At the end of the heavy processing code, use invokeLater to post a runnable which
4a. re-enables anything disabled in step 2.
4b. refreshes anything else necessary.

So, if your button runs a database query, you may want to prevent simultaneous queries 
(disable the button) or allow them.  When the request is finished on the other thread, 
invokeLater is used to refresh tables, graphs, histories, or even a status indicator.

Joel

-----Original Message-----
From: Ajit Bhingarkar x5495 [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, June 12, 2001 1:52 PM
To: Jose Antonio Lamas
Cc: Advanced-Swing
Subject: Re: Threads inside actionPerformed event


Hi Jose,

You can try following:

1. Create a Thread sub class and put all your heavy processing
in run() method.
2. Call start() on this class from your button's actionPerformed()
method.
3. This won't block the even thread during that heavy processing.

This will work only if you do not want to update the gui after your
processing is done.

Using invokeLater() won't help. as it just runs the  processing code in
even dispatch thread. In fact, you do not need (generally) to
use invokeLater() from even dispatch thread.

If you want to update the gui after processing, there is no other
way but to keep GUI frozen until your thread returns.

Hope this helps ..

- Ajit

Jose Antonio Lamas wrote:

> Hi again,
>
> I'm using JDK 1.3, and the problem is that the "actionPerformed" event
> blocks the GUI and I can't do anything else.
> I've tried with invokeLater(), and also invokeLater() within another created
> thread inside the "actionPerformed" event, but the GUI is freezed until the
> event is consumed.
>
> Any solution??
>
> Thanks
>
> > -----Mensaje original-----
> > De: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]En
> > nombre de Ajit Bhingarkar x5495
> > Enviado el: lunes 11 de junio de 2001 21:26
> > Para: Jose Antonio Lamas
> > CC: Advanced-Swing
> > Asunto: Re: Threads inside actionPerformed event
> >
> >
> > Jose,
> >
> > Which JDK are you using ?
> > invokeLater() is available from even dispatch thread.
> > Following is an exerpt from JavaDoc of invokeLater().
> >
> > If invokeLater is called from the event dispatching thread -- for
> > example, from
> > a
> > Button's ActionListener -- the doRun.run() will still be deferred
> > until all
> > pending
> > events have been processed. Note that if the doRun.run() throws
> > an uncaught
> > exception the event dispatching thread will unwind (not the
> > current thread).
> >
> >
> > Anyway, if you want your thread to return control to even thread after
> > heavy background processing, you can use thread.join().
> > But then, what's the use of using threading ?
> >
> > - Ajit
> >
> > Jose Antonio Lamas wrote:
> >
> > > Hi,
> > >
> > > I would like to know if there is any way to run a thread inside an
> > > "actionPerformed" event. The point is to return the control
> > after the button
> > > is pressed while the created thread is doing a background job.
> > The applet
> > > runs the thread but doesn't return the control, and "invokeLater" is not
> > > available inside the event.
> > >
> > > Example:
> > >
> > > jButton1ActionPerformed (java.awt.event.ActionEvent evt)
> > >
> > >         Runnable runner = new Runnable() {
> > >           public void run() {
> > >               try {
> > >                     // HEAVY BACKGROUND PROCESSING
> > >                 } catch(Exception e){
> > >                         System.out.println(e);
> > >                 }
> > >           }
> > >
> > >         //RETURN FASTLY CONTROL
> > >
> > > }
> > >
> > > Thanks
> > >
> > > _______________________________________________
> > > Advanced-swing mailing list
> > > [EMAIL PROTECTED]
> > > http://eos.dk/mailman/listinfo/advanced-swing
> >
> > _______________________________________________
> > Advanced-swing mailing list
> > [EMAIL PROTECTED]
> > http://eos.dk/mailman/listinfo/advanced-swing
>
> _______________________________________________
> Advanced-swing mailing list
> [EMAIL PROTECTED]
> http://eos.dk/mailman/listinfo/advanced-swing

_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to