• Post Reply Bookmark Topic Watch Topic
  • New Topic

Window closing

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to put a confirm dialog when the user clicks the right-top close button of a window (JFrame). The following code is only work for confirm closing - but not for cancelling . Any idea ?
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (!allfinished) {
int opt = JOptionPane.showConfirmDialog(dialog, "You have not finished your current session",
"Cancel Session", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if ( opt == JOptionPane.OK_OPTION) {
LevelFrame myLevelFrame = new LevelFrame();
myLevelFrame.setVisible(true);
this.setVisible(false);
} else if (opt == JOptionPane.CANCEL_OPTION) {
this.setVisible(true);
// keep the current JFrame visible
}
} else {
myLevelFrame.setVisible(true);
this.setVisible(false);
}
}
});
 
Valeria Zavallone
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I think the problem is in the close operation property set for the frame.
Is it Hide or Dispose?If Hide it should work because you do not really close the frame but make it hidden. If dispose, you force the frame to DIE so it can't be available after the closing event.
If you have just choosen Hide it may be a problem of event handling..closing happens when user clicks on the defined "x" button, then you want it to be showed after the event..It is not so logical.
Why don't you implement a close button by yourself...Copy the same code you wrote on the actionperformed event of the button. You have only to add the line "this.hide" when checking for the dialog option. Try it! Valeria
PS have a minute to give a look to my problem? My username is Valeria Zavallone. Please...
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Valeria,
Many thanks for your thoughtful reply.
I have gone through your posting, but have no suggestion. I have no knowledge about that.
Thanks again.
Sabbir
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the same problem you have to set the default close operation to do nothing on close.
if (choice == JOptionPane.CANCEL_OPTION) {
JFrame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!