Reputation: 61
For my custom JDialog
,
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
has been set.
There is a button on the JDialog
. Based on a condition, it calls dialog.dispose()
.
Both these actions trigger the windowClosed
event. But I want to identify it reached there because the close button was clicked or because the dispose method was invoked.
Upvotes: 6
Views: 5010
Reputation: 91
Add a WindowListener
to the JDialog
, and on windowClosed
set a boolean or something when it is closed. Also have a buttonClicked
boolean, if they clicked the button it would be true, if they clicked the exit button at the top of the window it would be false.
boolean closed;
boolean buttonClicked;
JButton exitbutton;
JDialog dialog;
...
public void actionPerformed(ActionEvent arg0) {
buttonClicked = true;
dialog.dispose();
}
...
dialog.addWindowListener(new WindowListener() {
public void windowActivated(WindowEvent arg0) {
// Do nothing
}
public void windowClosed(WindowEvent arg0) {
closed = true;
if(buttonClicked) {
//They cliked the button to close it.
} else {
// They didn't click the button, they clicked exit in the top right corner of screen.
}
}
public void windowClosing(WindowEvent arg0) {
// Do nothing
}
public void windowDeactivated(WindowEvent arg0) {
// Do nothing
}
public void windowDeiconified(WindowEvent arg0) {
// Do nothing
}
public void windowIconified(WindowEvent arg0) {
// Do nothing
}
public void windowOpened(WindowEvent arg0) {
// Do nothing
}
});
Upvotes: 6