Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JFrame window closing  RSS feed

 
Adithya Rayaprolu
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to present the user a confirmation dialog when he clicks the close button on the top right corner on the JFrame. For this I captured windowClosing event and the code I have in this function is as follows:
public void windowClosing(WindowEvent e) {
int choice = JOptionPane.showConfirmDialog ( mainFrame.contentPane, "Do you really want to exit?", "Exit System", JOptionPane.YES_NO_OPTION);
if(choice == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
But when I click No button in the confirmation dialog, the window is getting closed. I do not know why this is happening. Can somebody explain what I am doing wrong?
Thanks in advance.
 
Amit Agarwal
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Adithya,
the default operation of the JFrame when you give a close command is to hide the window. you can use JFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) to capture the windowClosing the way you want to. Now it will be up to you to close the frame. A simple System.exit() works fine.
Amit
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!