1

The goal of my code is to get the user input and when the user would like to save the data, they press the OK option. However, if the user has input some data, and would no longer like to continue, they then just press the red cross exit button via the top right corner to exit without saving?

How do I go about this?

Here's my code:

JOptionPane.showMessageDialog(null, myPanel, "Edit Fruit", JOptionPane.INFORMATION_MESSAGE);

...

if (!(JOptionPane.OK_OPTION))
{
   //If the user exits the option pane via the red cross, exit the loop.
   break;
}

EDIT: This code does not work as I'm only looking for when a JOptionPane.showMessageDialog is closed via the red cross and not an OK button:

addWindowListener(new WindowAdapter()
    {
        @Override
        public void windowClosing(WindowEvent e)
        {
            System.out.println("Closed");
            e.getWindow().dispose();
        }
    });

The code still executes even when I press the OK option in my JOptionPane. I only want to listen for the exit via the red cross.

Thanks for your help.

juiceb0xk
  • 949
  • 3
  • 19
  • 46
  • What do you want to achieve? I do not see a question. You want to detect it and then? – WesternGun Nov 30 '16 at 11:13
  • The question is my title, my program is not of relevance to this question. But I would like to break my for loop, as mentioned in my code snippet. – juiceb0xk Nov 30 '16 at 11:14
  • A quick search in Google with `swing window close listener` directs me to this answer: http://stackoverflow.com/questions/16295942/java-swing-adding-action-listener-for-exit-on-close – WesternGun Nov 30 '16 at 11:16
  • Awesome, thank you very much. Let me try this out. – juiceb0xk Nov 30 '16 at 11:17
  • The code does not work, as it still executes when I press both the OK button and red cross. I've edited my description to better explain the ordeal. – juiceb0xk Nov 30 '16 at 11:28
  • Now we have this....http://stackoverflow.com/questions/18715650/how-can-i-add-a-listener-on-the-ok-button-of-joptionpane – WesternGun Nov 30 '16 at 11:57

2 Answers2

3

Firstly, it's not a very standard UI to do something only if the user closes a window by the red x - as opposed to closing the window by typing Alt-F4, or double clicking the icon, or Alt-Space-C, and so on; so I'll assume you just meant closing without pressing the OK button rather than by the red x specifically.

You have code which can detect when the window is closed.

You have code can detect when the user presses the OK button.

So set a flag to indicate the OK button was pressed, and if the flag is not set when the window closed, then the user closed the window without pressing the OK button.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
1

Another quick search with the keywords "JOptionPanel close listener" takes me to another question.

How can I add a listener on the ok button of JOptionPane?

The accepted answer suggests to check the return value of the method showOptionDialog.

But you can use the method JOptionPane.showOptionDialog with a single DEFAULT_OPTION for the OK button. The showOptionDialog will return 0 if OK was clicked and -1 if the user closed the dialog.

int res = JOptionPane.showOptionDialog(null, "Hello", "Test",
JOptionPane.DEFAULT_OPTION,
    JOptionPane.INFORMATION_MESSAGE, null, null, null);

System.out.println(res); 

You don't need a listener because the javadoc says:

Each showXxxDialog method blocks the caller until the user's interaction is complete.

Here, there is no difference between close with rea cross, with key combinations, etc., as the first answer suggests. But, I guess maybe you don't need.

Community
  • 1
  • 1
WesternGun
  • 11,303
  • 6
  • 88
  • 157