0
int confirmDialog = JOptionPane.showConfirmDialog(null, "You selected quit! \nAre you sure you want to quit?", "Selected Quit", JOptionPane.YES_NO_OPTION);


if(confirmDialog == 0)                  
  System.exit(0);           
//focus under this comment here                     
if(confirmDialog == 1) {                    
  game.reset();                 
  displayBoard();                       
}

I made a game with a board. I have a quit button to quit the game. When the user clicks the button they are prompted with the JOptionPane above. It is asking them if they are sure they want to quit. I am able to close the game, but my problem is when the user selects "no". I want the user to be able to click "no" and have it so the game resets which it does, I can see the board reseting. But the optionPane does not go away and i want the OptionPane to go away so they can play.

dic19
  • 17,821
  • 6
  • 40
  • 69

1 Answers1

4

Since your confirm dialog has two possible outcomes you can write a simple if-else block like this:

int confirmDialog = JOptionPane.showConfirmDialog(null, "You selected quit! \nAre you sure you want to quit?", "Selected Quit", JOptionPane.YES_NO_OPTION);

if(confirmDialog == JOptionPane.YES_OPTION) {               
    System.exit(0);           
    //focus under this comment here                     
} else {                    
    game.reset();                 
    displayBoard();                       
}

Note the use of static field JOptionPane.YES_OPTION makes your code more robust.

I can see the board reseting. But the optionPane does not go away and i want the OptionPane to go away so they can play.

Check if something in game.reset() is blocking the Event Dispatch Thread (a.k.a. EDT). The EDT is a single and special thread where Swing components creation and update take place. If there's some time consuming task blocking the EDT your GUI may become unresponsive. See more in Concurrency in Swing trail.


Off-topic

About this line:

System.exit(0);

This method terminates the currently running JVM as stated in System.exit(int status) javadoc. This is actually a bad practice. When working with Swing you should call dispose() on your JFrame and that should terminate the JVM if there's no pending threads working.

dic19
  • 17,821
  • 6
  • 40
  • 69
  • public void reset() { status = GameStatus.NotOverYet; setEmpty(); layMines (10); } //That is everything in that method. I even tried commenting out game.reset() and displayBoard() and just had return; and the same problem occurred. – user3254746 Feb 05 '14 at 22:43
  • 1
    @user3254746 You may want to add your code in your question by clicking in [Edit](http://stackoverflow.com/posts/21589521/edit) option, so more people can see it. This method doesn't look complicated but what do `setEmpty()` and `layMines(10)` methods do? Do you see my point? I can't (nobody can) guess what is going on in your code without seeing it but it seems like the EDT is getting blocked in some place. – dic19 Feb 05 '14 at 22:48