wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Swing appears to Freeze Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing appears to Freeze" Watch "Swing appears to Freeze" New topic
Author

Swing appears to Freeze

Rob Grubb
Greenhorn

Joined: Aug 16, 2009
Posts: 13
Hi there everyone!

I'm writing a gui for a program i wrote for an assignment at uni. The problem i am having is that when i compile and run it from. Sometimes it works perfectly and other times it appears to freeze (None of the buttons are working). But as soon as a minimize and maximize it or drag it to a second monitor. Its no longer frozen. Its doing this randomly. I would say about 20% of the time. I tried a call to repaint and pack at the end of jpanel and jframe constructor but that does not work.

Anyone have any ideas?
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Rob,

It's difficult to tell without actually seeing the code. But, one thing to look at for sure is if you're doing any GUI updates that are not on the event dispatch thread (EDT)? The swing threading model requires that all GUI updates are on the EDT and not doing so can cause odd GUI behavior (and since it's a threading issue it may not happen all of the time). Another thing to verify is that you're not getting any exceptions (if you're running a GUI app with no console, it is possible you're just not seeing the exception - be sure to install an uncaught exception handler).

Jeff


Jeff Storey
Software Developer
[url]http://jeffastorey.blogspot.com[/url]
Rob Grubb
Greenhorn

Joined: Aug 16, 2009
Posts: 13
Hey Jeff,

Not getting any errors on the console. Never heard of the EDT so i will look into that and report back here. What if i call whatever is being called when the JFrame is minimized and maximized or moved to another screen? Anyone know whats being called there?

Rob
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2156
    
    7
Rob Grubb wrote:
Never heard of the EDT
Rob


Read the section from the Swing tutorial on Concurrency.
Rob Grubb
Greenhorn

Joined: Aug 16, 2009
Posts: 13
Thats a good link. Im loading and resizing quite alot of images and insantiating a bit of objects in my jpanel thats giving me problems. So if i take these tasks and put them into a worker thread and have it run before the jpanel is created. do you think ill be good to go? will try this when i get home.
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
What if i call whatever is being called when the JFrame is minimized and maximized or moved to another screen? Anyone know whats being called there?


Not sure what you mean here.


Im loading and resizing quite alot of images and insantiating a bit of objects in my jpanel thats giving me problems.

Have you tried scaling back to a smaller number of objects/images to see if that helps and then gradually adding things back to see where the problems are? If you're doing a lot of image loading, be sure you're closing streams properly too.
Rob Grubb
Greenhorn

Joined: Aug 16, 2009
Posts: 13

What if i call whatever is being called when the JFrame is minimized and maximized or moved to another screen? Anyone know whats being called there?




Not sure what you mean here.


I just mean that moving the JFrame to another screen seems to fix the freeze. So if i manually call the methods invoked during a drag to a new screen at the end of the constructor. Wont this fix it?

Have you tried scaling back to a smaller number of objects/images to see if that helps and then gradually adding things back to see where the problems are?


Yup ive tried this. Doesn't matter if im loading 200 or 5. It freezes randomly sometimes no matter what the image load amount.

If you're doing a lot of image loading, be sure you're closing streams properly too.


The images are loaded at the first screen in some model objects. Only get the freeze error when i login to the app and go to the user area where the images are then resized.


I cant post all of the code here for the jPanel because its around 600 lines but heres whats being called in the constructor.

1. SetUp Basic instance variables
2. get images needed from model objects and resize them
3. initComponents method with default images and text
4. set all of the resized images onto the gui.
5. resize jframe and set its normal settings.

using netbeans.

Trying to implement a swing worker thread to handle the image resizing to see if this solves the problem. Do i put items 3, 4 and 5 in the done() method?
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Sounds more and more like an event dispatch thread issue. When you minimize/maximize the frame, the components are re-laid out (the layout is invalidated) and repainted. As for what gets performed in the done method, anything that does the actual updating of the GUI should be done there. SwingWorker is used for long running tasks, so the doInBackground method does any heavy lifting and then done just updates the GUI with the results. If you're just doing a lot of GUI updating but you don't need to send the task off to a background work, you can invoke tasks on the EDT by using SwingUtilities.invokeLater or SwingUtilities.invokeAndWait.
Rob Grubb
Greenhorn

Joined: Aug 16, 2009
Posts: 13
The SwingWorker class has fixed the problem! I'm not sure how though. I put the image resizing into a worker and called all the gui stuff in done and i havnt seen it freeze in over 200 compiles.

Thanks guys!
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Rob,

Glad to hear it worked. It's because you stopped doing GUI updates from the main thread.

Jeff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing appears to Freeze
 
Similar Threads
get current width of DIV element in JavaScript
snow!!!
WA #1.....word association
Clinton 'threatened' N Korea over nuclear arms
How to apply a tooltip to tabs on a DecoratedTabPanel