[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [ajug-members]: Wait()



OK,

Here's the deal:  with Swing, GUI events are processed on a single thread.  
You
don't want to run any intensive processes on that thread, because the GUI 
will
not render properly.  For example, you might press a button, and it will 
stay down
until the task that it triggers is done.  So the idea is that you put your 
long tasks on
a new thread.

On the flip-side, you don't want to run the GUI on any threads except the 
GUI thread (Event-Dispatch Thread).

The problem is that when you start a separate thread to do some work, you 
might want
to update the GUI, but we just said not to run GUI code on anything but the 
GUI thread.
The answer is to use a special function to push the GUI calls onto the GUI 
thread from
your worker thread.

You use a 'worker' thread to run your processes, and if your 'worker' thread 
needs to talk
to the GUI, then you wrap the calls in a thread and put them on the GUI 
thread.

There are two classes that will make your life easier:
SwingWorker - For running your long processing tasks.
SwingUtilities -  For calling GUI code from your/other threads.

Put your code in the construct() method of your subclass of SwingWorker,
and if it needs to call the GUI, wrap the call in the run() method of your
Runnable implementation and put that Runnable class on the event-dispatch
thread using SwingUtilities.invokeAndWait(runnable) or 
invokeLater(runnable).

Alas, there is a tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html

Unfortunately, this seems like overkill for just getting started with swing. 
  I can assure you that
this is one of the more difficult things about Swing, but it really makes it 
effective.

You could use a Timer within a window whose task is to close the window.  If 
you are truly going
for a timed splash screen.  It might make more sense to show a splash screen 
while
resources are loading, and use the timer to ensure a minimum display time, 
to make
sure the user sees product advertisement or copyright info, etc.

>I am new to JAVA, so please bare with me.  If you don't use
>Thread.sleep(long) for displaying a wait screen, what would you recommend
>doing to have a form displayed for 30 seconds then show a new window?  
>Also,
>the hide and show functions do not seem to work.  I thought you could just
>use splash.hide(); and fserver.show();, but that will not compile.
>
>Any help would really be appreciated,
>
>What I have so far:
>
>public static void main(String args[]) {
>
>         new fserver().hide();
>         new splash().show();
>         wait 30 seconds
>         try {
>             Thread.sleep(300); // milliseconds
>         } catch (InterruptedException e) {}
>         new splash.hide();
>         new fserver().show();
>     }
>
>
>
>Cody Wehunt
>cody@wehunt.net
>770-402-9189 Cell
>770-226-5938 Work
>My  as always.
>
>-----Original Message-----
>From: Brian Lee [mailto:brian_a_lee@hotmail.com]
>Sent: Monday, April 26, 2004 5:17 PM
>To: ajug-members@ajug.org
>Subject: RE: [ajug-members]: Wait()
>
>You don't need to include anything since Thread is in the java.lang 
>package.
>
>Please note, that you really shouldn't use Thread.sleep(long) for 
>displaying
>a wait screen to a user as this would be a UI function. Usually, I've found
>most of the time when you think you need a Thread.sleep() call, you really
>don't (or shouldn't).
>
>BAL
>
> >From: "Cody Wehunt" <cody@wehunt.net>
> >To: <ajug-members@ajug.org>
> >Subject: RE: [ajug-members]: Wait()
> >Date: Mon, 26 Apr 2004 16:58:56 -0400
> >
> >Do you have to setup any threads to use it, or can it be used say to
> >show a splash screen for x seconds and then continue processing?  Do
> >you have to declare any includes?
> >
> >
> >Thanks,
> >
> >
> >Cody Wehunt
> >cody@wehunt.net
> >770-402-9189 Cell
> >770-226-5938 Work
> >My   <http://wehuntnet.instaccount.net/images/two_cent.gif> as always.
> >
> >
> >
> >   _____
> >
> >From: Jim Worthington [mailto:jim.worthington@worthsoft.com]
> >Sent: Monday, April 26, 2004 4:41 PM
> >To: ajug-members@ajug.org
> >Subject: RE: [ajug-members]: Wait()
> >
> >
> >Thread.sleep(30000L) is the equivalent. The argument is a long and the
> >units are milliseconds.
> >
> >- Jim Worthington
> >
> >-----Original Message-----
> >From: Cody Wehunt [mailto:cody@wehunt.net]
> >Sent: Monday, April 26, 2004 4:24 PM
> >To: ajug-members@ajug.org
> >Subject: [ajug-members]: Wait()
> >
> >
> >Is there a built in wait function in JAVA?
> >
> >i.e.
> >
> >wait(30); //hold processing for 30 seconds
> >
> >
> >Thanks,
> >
> >
> >Cody Wehunt
> >cody@wehunt.net
> >770-402-9189 Cell
> >770-226-5938 Work
> >My   <http://wehuntnet.instaccount.net/images/two_cent.gif> as always.
> >
> >
> >
>
>_________________________________________________________________
>Lose those love handles! MSN Fitness shows you two moves to slim your 
>waist.
>
>http://fitness.msn.com/articles/feeds/article.aspx?dept=exercise&article=et_
>pv_030104_lovehandles
>

_________________________________________________________________
Watch LIVE baseball games on your computer with MLB.TV, included with MSN 
Premium! 
http://join.msn.com/?page=features/mlb&pgmarket=en-us/go/onm00200439ave/direct/01/