473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Can I run 2 frames in separate threads

LM
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.

If this can be done, would it be possible to provide a code snippet?

Thanks

LM
Jul 17 '05 #1
2 4900
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.
Jul 17 '05 #2
Raymond DeCampo wrote:
LM wrote:
I have had no success with this, so would really appreciate some
ideas (or
insight). For the record, I am not a GUI programmer.


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.


The swing website has a great page on this showing the SwingWorkerThread
class. I have a project, http://swingutil.java.net, that includes a
modification to this class to make it do a little more. Here is how you
would use it to perform an action that a button
press has requested.

final JButton b = new JButton("Do it");
b.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ev) {
new ComponentUpdateThread( b ) {
public Object construct() {
...talk to server, read file etc...
return something;
}
}.start();
}
});

ComponentUpdateThread. will disable all buttons/actions passed into the
constructor. It also supports Action and JFrame/JDialog. There are
several other things that it does for you, see the docs. I've found
this to be a very convenient way to manage threading in GUIs.

Gregg
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bob Greschke | last post by:
I have a program that creates one or more frames with TF = TopLevel(Root) talks to some equipment, calculates some stuff, draws a graph on a canvas in the frame, then exits. The frame/s goes...
0
by: Rod Stephenson | last post by:
I am developing an engineering application for modelling multiphase flows in large pipe networks, and have just discovered the joy of pyopengl for 3D visualization. However the incantation I'm...
3
by: Andreas Müller | last post by:
i need two loops that run forever. one of it receives data and stores it to a vector. the other one writes the elements of the vector to the disk. this means the vector is a receiver buffer. How...
22
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
2
by: Shahid Siddiqui | last post by:
I want to know that whether the asp.net pages are served through separate threads or separate processes?
8
by: Angel | last post by:
Hi everybody, I am new to ASP.NET, and my question might be obvious to most of you but I do not seem to find many things about threads and ASP.NET. I have an object(Object 1) which need some...
11
by: Charles Law | last post by:
I know I have brought this one up before, but I didn't get an answer last time, so hopefully I will have better luck this time. I send data out of a serial port on my main thread. I wait for a...
9
by: koschwitz | last post by:
Hi, I hope you guys can help me make this simple application work. I'm trying to create a form displaying 3 circles, which independently change colors 3 times after a random time period has...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.