0

I have a JOptionPane set when a certain action happens, and I want it to be showMessageDialog because that just displays a message. How do I make it so when you click OK or cancel, it closes the entire application?

This is what I put

JOptionPane.showMessageDialog(null, "you lose");
if (JOptionPane.OK_OPTION){
        System.exit(0);
}

but it doesnt work. I based it off of this code I found online:

int exit = JOptionPane.showConfirmDialog(mainFrame, "Are you sure?");

if (exit == JOptionPane.YES_OPTION)
{
    try
    {
        Runtime.getRuntime().exec("cmd.exe /c start www.yahoo.com");
    }
    catch (Exception exception)
    {
    }
    System.exit(0);
}
else {
}

But that is for a Confirm dialog and I just want it to work for Message dialog.

I tried changing JOptionFrame.showConfirmDialog to JOptionFrame.showMessageDialog but it doesnt work, as int and other stuff had to be deleted.

Thanks

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
Alonzo Robbe
  • 465
  • 1
  • 8
  • 23

2 Answers2

2

You can use ConfirmaDialog and use the INFORMATION_MESSAGE type.

int exit = JOptionPane.showConfirmDialog(null, "Are you sure?" , null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (exit == JOptionPane.YES_OPTION)
{
    //Do smt
}

If you will close the app any way, just do this:

JOptionPane.showMessageDialog(null, "you lose");
System.exit(0);

Why need to bother if in any cases, you will close the app?

Pham Trung
  • 11,204
  • 2
  • 24
  • 43
  • yes it worked. Thanks! Its just it looks a little odd b/c my purpose for the button is to inform the player they lost and accept it by clicking OK and it closes. Here there is a Cancel option, which I suppose can be coded to close the app, but it just looks like a weird design choice, you know what I mean? Is there really no way around this? – Alonzo Robbe Jun 09 '15 at 09:53
  • @RobBor it is perfectly fine to have a cancel option, if you want to give user control, whether they want to close it or not. But if this is no choice, so you don't need to care about the option at all, just close it, why bother? show the message, after they clicked, close the app. – Pham Trung Jun 09 '15 at 09:56
  • How do I do that? Sorry I am an amateur. NVM, I just saw your original post. Thats what I wanted. Thank you! I am sorry for any troubles – Alonzo Robbe Jun 09 '15 at 10:01
  • would you be able to help with another problem I am facing? The place I tested the code is when the player bullet hits an enemy. Not the correct place to test, but it was the first place I tried. Well, it works fine there, but nothing actually happens when the player actuallly dies, Ive been having this problem for some time now, I made another thread hoping to get it answered, but to no avail. Would you somehow happen to know what to do? When I use your edit in the correct place, under an if statement, it just opens and closes immediately. – Alonzo Robbe Jun 09 '15 at 10:16
  • @RobBor can you create a new question, and give me the link, I think it is better to see actual code :) – Pham Trung Jun 09 '15 at 10:19
  • I had to create a new acc, hehe. But [here it is](http://stackoverflow.com/questions/30729651/why-does-nothing-happen-when-a-if-0-criteria-is-met) – Alonzo Robbe Jun 09 '15 at 10:38
0

Try this:

int choice = JOptionPane.showConfirmDialog(null, "you lose", "title", JOptionPane.OK_CANCEL_OPTION);
if (choice == JOptionPane.OK_OPTION || choice == JOptionPane.CANCEL_OPTION){
   System.exit(0);
}

Thanks.

m3nation
  • 206
  • 2
  • 7
  • Yes this works! I'll just say what I wrote above, It just it looks a little odd b/c my purpose for the button is to inform the player they lost and accept it by clicking OK and it closes. Here there is a Cancel option, which is coded to close the app, but it just looks like a weird design choice, you know what I mean? Is there really no way around this? – Alonzo Robbe Jun 09 '15 at 09:55
  • Have a look [here](http://stackoverflow.com/questions/11204878/joptionpane-showconfirmdialog-with-only-one-button) – m3nation Jun 09 '15 at 11:07