Is a JFrame threaded?

B

Ben

Hi, just wondering if anyone knows whats going on here>

- I have a class that creates a drawable JPanel within a JFrame.
- The class then runs a method that reads in a file, line by line and
uses the lines to changes various objects within the class.
- After each line the JPanels repaint method is called.

This works fine. However I created another JFrame, made visible by a
button on the first JFrame. The JFrame captures a file name and calls
the method to read in the file and make the changes, using the captured
file name. However the JFrame just freezes. I thought this maybe had
something to do with threading. So I let the method be called by the
constructor as it was done originally. However I changed this to use
two while loops. The first never end as the second only starts when
the filename is changed from "". This works fine, however the program
is noticably slower at responding to various things.

Is this anything to do with threading? This What is a better solution?


Thanks.

Ben.
 
O

Oliver Wong

Ben said:
- I have a class that creates a drawable JPanel within a JFrame.
- The class then runs a method that reads in a file, line by line and
uses the lines to changes various objects within the class.
- After each line the JPanels repaint method is called.

This works fine. However I created another JFrame, made visible by a
button on the first JFrame. The JFrame captures a file name and calls
the method to read in the file and make the changes, using the captured
file name. However the JFrame just freezes. I thought this maybe had
something to do with threading. So I let the method be called by the
constructor as it was done originally. However I changed this to use
two while loops. The first never end as the second only starts when
the filename is changed from "". This works fine, however the program
is noticably slower at responding to various things.

Is this anything to do with threading? This What is a better solution?

Can you post an SSCCE? http://mindprod.com/jgloss/sscce.html

- Oliver
 
B

Ben

First line should say.

Create an instance of the MainClass to run the program. (I have been
using a program called BlueJ)
 
T

Thomas Hawtin

Ben said:

SSCCE should usually weigh in at less than sixteen megs...
The main class to create an instance of to run in MainClass. When
run_sim is ran from the constructor its fine. However the action
listener tries the window freezes.

It looks at first glance as if you have created and shown the JFrame and
run your blocking code on the main thread. For the action listener you
have run the blocking code on the AWT Event Dispatch Thread (EDT).

You should not create Swing components off the EDT. Typically your main
method should look something like:

public static void main(final String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
doSwingStuff(args);
}
});
}

Once you do that, you should notice your blocking code blocks the GUI.
You need to run the blocking code on a different thread, and then feed
the results back through to the EDT (with EventQueue.invokeLater). For
details google for Swing threads tutorials.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,798
Messages
2,569,649
Members
45,382
Latest member
tallzebra

Latest Threads

Top