• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

close button in frame????

Hi guys. How would i trap in frame so that when a user click the X button(AWT Frame), i will issue a message asking "Do u really want to exit?"..

thanks..
0
rdy1437
Asked:
rdy1437
1 Solution
 
objectsCommented:
Add a window listener to the frame, and prompt the user in the windowClosing() method.
0
 
udaykumar22Commented:
hi,

Try this...it works...

===========================================================

JFrame JF = new JFrame("My Frame");

JPanel JPParent = new JPanel();

JF.getContentPane().add(JPParent, null);

JF.addWindowListener
(
 new java.awt.event.WindowAdapter()
 {
  public void windowClosing(WindowEvent WE)
  {
   int ChosenOption = JOptionPane.showConfirmDialog
(
 JPParent,
 "Do you really want to exit?",
 "Exit Screen",  
 JOptionPane.OK_CANCEL_OPTION
);

   if(ChosenOption == JOptionPane.CANCEL_OPTION)
   {
// Do nothing is user presses 'Cancel'
    return;
   }
   else
   {
// Do whatever operation when you want is user presses 'Ok'...in this case --- Exits the application
    System.exit(0);
   }
  }
 }
);

===========================================================

Regards,
Uday.
0
 
objectsCommented:
Thanks for elaborating on my original comment :)
0
 
vemulCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- split points between objects and udaykumar22

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 
Computer101Commented:
Objects, Look for your question in this topic area

Computer101
E-E Admin
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now