I think you are waiting for the user to press the button, constantly checking which button is pressed.
"I need to make him wait for the OK or Cancel button to click without freezing the form / dialog."
Maybe you should use evens to get a notification when a user clicks on something without waiting for him to click a button - maybe there is some OnWindowExit event?
Or maybe something like this:
MyPanel panel = new MyPanel(...); int answer = JOptionPane.showConfirmDialog( parentComponent, panel, title, JOptionPane.YES_NO_CANCEL, JOptionPane.PLAIN_MESSAGE ); if (answer == JOptionPane.YES_OPTION) {
Otherwise, you can see how to handle window events, especially windowClosing(WindowEvent) here
karla source share