Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

overriding Close operation in WindowAdapter

Thread view: 
Sameer Shinde - 19 Mar 2005 19:08 GMT
Hello,
I want to confirm the closing of frame with a Yes/No Dialog.
How do I override the Close operation of windowClosing method of
WindowAdapter class?

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
int choice = showYesNoDialog(getTitle(),
"You are about to close " + getTitle() + ".\n"
+ "Are you sure you want to do this?");

if (choice == 0) {
System.exit(0);
}
else
{
//what to write here?
// evt.getWindow().getOwner().setVisible(true); ????????
//If we put a null statement it will close the frame.
}
}
});
Please help,
-Sameer
Anthony Borla - 19 Mar 2005 20:53 GMT
> Hello,
> I want to confirm the closing of frame with a Yes/No Dialog.
[quoted text clipped - 18 lines]
> }
> });

You need to alter the default 'window close' behaviour. This is done via:
   ...
   yourFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
   ...
   yourFrame.addWindowListener
   (
     new WindowAdapter()
     {
       public void windowClosing(WindowEvent e)
       {
            ...
            // Dialog, whatever ...
            ...
            if (choice == 0) System.exit(0);

            // No need to do anything else here ...
       }
     }
    );

See:

http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html#window
events

for more details.

I hope this helps.

Anthony Borla
John - 20 Mar 2005 01:29 GMT
Also, don't do it like this, ie don't close the application from the
dialog. Create a method
that displays the dialog and returns an identifier for the button that
was pressed, for example
you can return 0 if the Cancel button or the close button has been
pressed and 1 for the Ok
button, then, depending on the returned value, you can close the
application
from the method that calls the display method for the dialog. You'll
have to display a modal
dialog to do this, otherwise you won't have the value returned
synchronously.

>>Hello,
>>I want to confirm the closing of frame with a Yes/No Dialog.
[quoted text clipped - 49 lines]
>
> Anthony Borla


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.