The showMessageDialog method returns void when the user closes or clicks ok. But you can use the JOptionPane.showOptionDialog method with a single DEFAULT_OPTION for the OK button. showOptionDialog will return 0 if it clicked OK 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 javadoc says:
Each showXxxDialog method blocks the caller until user interaction is complete.
cyon source share