problems with windowClosing in windowAdapter - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

problems with windowClosing in windowAdapter

yanghui1023yanghui1023 Posts: 24Member
I used JFrame and want to add windowlistener to a JFrame object.I overrided the windowClosing method in windowAdapter.But I don't want the JFrame object always closed. I want the system will exit in some cases,and the system won't in other cases.
My code:

[italic]
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e){
if (isModified==true) { //there are some changes of the file
String message="Your file has been modified,do you want to save the changes?";
int result=JOptionPane.showConfirmDialog(this, message, "save?", JOptionPane.YES_NO_CANCEL_OPTION );
if (result==JOptionPane.CANCEL_OPTION) { //don't exit
return; //don't really exit
} else if (result==JOptionPane.YES_OPTION){ //
save(); //the save block
}else { //I dont want to save the changes
System.exit(0);
}
}else { // there are't any changes of the file
System.exit(0);
}
}
});
[/italic]

I watched the task manager.In fact,if just override the windowClosing method while do nothing in the method ,such as:

[italic]

this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e){

}
});

[/italic]

The frame don't really been killed but it still owns resources, but I can't see the JFrame object.
Can anyone tell me the reason? How can I kill the frame in some cases when the windowClosing method is called while the freme is not killed and can be seen in some cases?
Thanks!

Sign In or Register to comment.