• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing appears to Freeze

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rob Grubb
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Grubb wrote:
Never heard of the EDT
Rob



Read the section from the Swing tutorial on Concurrency.
 
Rob Grubb
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,

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

Jeff
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic