Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jeremy Carter
Greenhorn
Posts: 4
Android Java Windows Vista
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeremy Carter
Greenhorn
Posts: 4
Android Java Windows Vista
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic