aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Why won't my app wait for a button press before quitting? 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 "Why won Watch "Why won New topic
Author

Why won't my app wait for a button press before quitting?

Jeremy Carter
Greenhorn

Joined: Dec 10, 2011
Posts: 4

I'm just finishing up a Java intro course, and working on my final project. My app is a Java implementation of Yahtzee, and I'm calling it Jahtzee. All I've got so far is some of the initial gui screens created and laid out, and I'm already having a problem I just can't seem to get past. I've spent the last couple hours googling it, but found no solution.

For the purposes of this post, I've cleaned out most of the unrelated code, and just left a skeleton that still displays the issue.
I create a frame, with a panel in it containing a "Quit" button. I want the app to display the frame, and wait for the quit button to be pressed, and then quit. I've obviously not done something right, as the app doesn't wait for the Quit button before continuing on. There is a commented "Thread.sleep(10000);" line in there, and if I uncomment it, then the quit button shows up, and functions correctly, as long as I press it before the 10 seconds have elapsed.

I welcome any comments on what my problem could be, as well as suggestions for improving the logical layout of the app.

Thanks,
Jeremy

JahtzeeApp class (1 of 4)


JahtzeeWindowManager class (2 of 4)


JahtzeeFrame class (3 of 4)


JahtzeePanel class (4 of 4)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

When calling setVisible(true) on a frame this will not block, and immediately return. Usually this doesn't matter, because the Event Dispatcher Thread (EDT) will still be active. Only when this thread (and all other non-daemon threads) have ended will the JVM end. However, you call System.exit(0), and this will immediately close the JVM. Remove this one line and your program will show its frame.

You should read Concurrency in Swing though, as you should run all GUI code on the EDT, even the creating part.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeremy Carter
Greenhorn

Joined: Dec 10, 2011
Posts: 4

Thanks so much Rob!

Removed the System.exit(0);

I can't believe it was so simple, but now that you explain it, it makes perfect sense!

I'll take a look at the Swing concurrency link as well.

Thank you SO much for the reply.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why won't my app wait for a button press before quitting?