Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Is a JFrame threaded?

Thread view: 
Ben - 21 Apr 2006 18:44 GMT
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.
Oliver Wong - 21 Apr 2006 20:22 GMT
> - 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
[quoted text clipped - 12 lines]
>
> 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
Ben - 21 Apr 2006 23:53 GMT
I have uploaded a SSCE @
http://www.geocities.com/benspencer_2000/ssce.zip

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.

Any help is greatly appreciated.

Thanks!!

Ben.
Ben - 21 Apr 2006 23:57 GMT
First line should say.

Create an instance of the MainClass to run the program.  (I have been
using a program called BlueJ)
Thomas Hawtin - 22 Apr 2006 16:28 GMT
> I have uploaded a SSCE @
> http://www.geocities.com/benspencer_2000/ssce.zip

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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.