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

删除回忆录丶 提交于 2019-12-05 06:08:54

The showMessageDialog method returns void when the user closes or clicks ok. 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.

When the button on JOptionPane is clicked, it returns the index value of button. By checking the value, you can get to know that Ok button is clicked or not.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!