LM wrote:
Hi,
Assume I have a java.awt.Frame running in my main Thread. This frame is the
main UI for my application. Is it possible for this application to create a
second frame in a completely separate thread, so that if the main thread
sleeps (Thread.sleep(msec)), the second frame is not affected and can still
accept user input (keyboard and mouse etc.)?
My problem at the moment is if the main thread sleeps, the main UI is locked
until the sleep finishes (which is as expected), but the second frame also
locks (since it is a part of the same thread).
I have had no success with this, so would really appreciate some ideas (or
insight). For the record, I am not a GUI programmer.
You need a couple of perspective changes relative to this area of the
code. First, a Frame does not run in a thread. The JFC sets up one
thread to handle GUI events. (To be honest, most of what I saying is
based on my understanding of the Swing classes, but it should apply to
AWT as well.) No matter how many Frames you have, there is only one GUI
thread. So when the user does something like press a button, the
ActionListener is executing within the GUI thread. As you discovered,
if you sleep within this thread the GUI becomes non-responsive. If you
must do something like sleep or a long calculation, it is best to create
a new thread and perform the action within it. For example:
public void actionPerformed(ActionEvent e)
{
new Thread(new MyRunnable()).start();
}
public class MyRunnable implements Runnable
{
public void run()
{
// Do stuff here
}
}
HTH,
Ray
--
XML is the programmer's duct tape.