aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Stuck in a loop after setVisible false to JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Stuck in a loop after setVisible false to JFrame" Watch "Stuck in a loop after setVisible false to JFrame" New topic
Author

Stuck in a loop after setVisible false to JFrame

Eitan Shaked
Greenhorn

Joined: Dec 01, 2012
Posts: 3
Hi all,

I'm tryiing to write a program for ordering movie tickets.
I'm stuck on the first phase
The problem is when I want to set JFrame visible to false, the programs doesnt continue running.
It's more strange that when I'm printing something in the loop that checks the wndow visiblilty it continue running and actually works.
Another strage thing is that in debugmode it also works fine.
So the problem occues only when tring to run the program not in Debug mode and without a print in the loop.
Any help will be appriciated
Here are the classes codes :


Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
The problem is that when you call setVisible() the displaying of the frame is done on the EDT (Event Dispatch Thread) and not the main thread so the frame may not be displayed yet when the method returns. You then enter your while loop and so the while loop immediately fails and you dispose of the frame. It works in debug because as you step through the lines manually there is time for the Frame to be displayed before you execute the while loop check. Your second loop may work because the relevant code has already been loaded and executed once and so completes quickly enough second time around for the while loop check to succeed. But this may vary for multiple runs of the code or if running it on another machine.

If you want to display a GUI and get the entered value when the GUI closes you are better off using a modal JDialog.
Eitan Shaked
Greenhorn

Joined: Dec 01, 2012
Posts: 3
Hey,
Thanks for the fast reply..
But, if you were right, then the print that comes after the shile should have print what inside of it, but it doesnt go there.. It looks like the program is stil running and hangs.
I would like you to show an example of how to use it correctly in JDialog.
Thanks again
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
selectedMovie is an empty string if the while loop doesn't run and so how would you tell if it was printing anything or not? Try initialising it a "TEST".

To find out how to use JDialog check out the Java tutorials.
Eitan Shaked
Greenhorn

Joined: Dec 01, 2012
Posts: 3
Printing messages really helped..
But the problem was solved in other way..
To overcome this issue I called repaint() function, which actually waits till all components are drawn. After that everything works great
Thanks for the help though, I will definitely use your forum , and will share my knowledge in other programming languages ( which I'm well known with )
Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
And welcome to the Ranch
 
 
subject: Stuck in a loop after setVisible false to JFrame