JFrame addWindowListener doesn't work -- alternative close button action
Hello,
I am trying to get a program to save some data to a DB before exiting; So I need to assign a customer listener for the close button of the program.
This is that what I have tried so far:
privatevoid initComponents(){
this.addWindowListener(new WindowAdapter(){
publicvoid WindowClosing(WindowEvent e){
JOptionPane.showMessageDialog(thisInstance,"Window Event Handled Properly");
}
});
This Dialog never gets displayed when attempting to close the parent JFrame ("this"); which means the WindowListener isn't working. What's wrong?
Thanks
[936 byte] By [
frost986a] at [2007-11-27 11:56:27]

# 2
try using windowListener than windowAdapter
public aFrame extends JFrame implements WindowListener {
public aFrame(){
addWindowListener( this );
}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosing(WindowEvent e){
// your code
}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
}
# 3
First, it's "public void windowClosing(...)", not "WindowClosing". Java is case sensitive, and using WindowAdapter certainly masks this problem.
Also, you maybe want to be calling
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
and having your window listener close/dispose the frame based on some condition (dialog response, DB saving completion, etc.)