• 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

Implement showOptionDialog - create new game

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am working on a game Minesweeper.
In there I have implemented a showMessageDialog and I would like to replace it with a showOptionDialog.
The problem is, to define the correct action after clicking Yes or No...

Thats how it is now with the showMessageDialog:


How do I have to implement the showOptionDialog to create a new game when the user clicks Yes and quit the application when the user clicks No?


I appreciate any advise and help...
I can post the whole section of the class if the code extract is not enough (was not allowed to add it as attachment).

Thank you
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the API JOptionPane#showConfirmDialog returns an int which indicates the user choice. The available choices are defined as static constants in the JOptionPane class itself. e.g. YES_OPTION, CANCEL_OPTION
Check the value of the choice and proceed accordingly.

Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
 
peter meyer
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html



Thank you Maneesh...
I already read the recommended oracle article. Because of that I was able to implement a little bit of the showOptionDialog.

When I say No or cancel the dialog the application is closing; so far so good.
I struggle to rebuild the application if I say Yes on the dialogue. The application quits as well when I say Yes (play again)...
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

peter meyer wrote:I struggle to rebuild the application if I say Yes on the dialogue. The application quits as well when I say Yes (play again)...



Must be something to do with the changes you made, then. If you're still stuck would you care to post that part of the new code?
 
peter meyer
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paul taking time for this. Here is the code:


(don't get confused with some German expressions or names in the code).

Thanks
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if (explodiert==1)
That looks as if you were writing C code where you use 0 for false and non‑zero for true. You don't in Java®. You either
  • 1: Make explodiert a boolean in which case you would write if (explodiert)...     or
  • 2: You create an enum to match explodiert so you might write if (explodiert == SEVERELY)... or if (explodiert == SLIGHTY)... or if (explodiert == NOT_AT_ALL)...
  • Note you should put spaces around the == operators.
    Another thing I can see is that your method is much much too long. Your if statements are difficult to follow and you will be unable to understand any of them in six months. That method should be divided into much smaller methods. Or you should think of a more object‑oriented solution. I am afraid I can't think of one at present, but you can do all sorts of things. You can cover the screen with multiple buttons, each representing a particular location. I presume you have Mine and Minesweeper classes. Think of how to match a Mine to its location. Do you have any method for recording location on your screen?

    More about ==.
    Never write == true and == false which are poor style and error‑prone because you might write = instead by mistake.
    Never
    if (b == true)...
    Always
    if (b)...
    Never
    if (b == false)...
    Always
    if (!b)...
    Notice the bang sign ! in the last statement.
     
    peter meyer
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you Richie...
    Well it seems I will go back to an earlier version I was working on...
     
    reply
      Bookmark Topic Watch Topic
    • New Topic