How can a Swing WindowListener veto JFrame closing
Solution 1
The right way is set JFrame.setDefaultCloseOperation
to DO_NOTHING_ON_CLOSE
when the window is created. And then just calling setVisible(false)
or dispose()
when your user accepts the close, or doing nothing when the close isn't accepted.
The whole purpose of JFrame.setDefaultCloseOperation
is only to prevent the need to implement WindowListeners
for the most simple actions. The actions performed by these default close operations are very simple.
EDIT:
I've added the solution I'm describing. This assumes you want the frame to be fully deleted.
frame.setDefaultCloseOperation(setDefaultCloseOperation);
frame.addWindowListener(new SaveOnCloseWindowListener(fileState));
...
public class SaveOnCloseWindowListener extends WindowAdapter {
private final FileState fileState;
public SaveOnCloseWindowListener(FileState fileState) {
this.fileState = fileState;
}
public void windowClosing(WindowEvent e) {
if (fileState.onQuit())
frame.dispose();
}
}
Solution 2
Closing an Application might make the process a little easier for you.
Duncan McGregor
Updated on June 14, 2022Comments
-
Duncan McGregor almost 2 years
I have a frame, and want to prompt when the user closes it to save the document. But if they cancel, the frame shouldn't close.
frame.addWindowListener(new SaveOnCloseWindowListener(fileState)); ... public class SaveOnCloseWindowListener extends WindowAdapter { private final FileState fileState; public SaveOnCloseWindowListener(FileState fileState) { this.fileState = fileState; } public void windowClosing(WindowEvent e) { if (!fileState.onQuit()) cancelClose(); } }
FileState looks at whether the document is dirty. If it isn't it does nothing and returns true. If it is dirty, it asks the user if he wants to save (YES/NO/CANCEL). If the user cancels at this point, it should abort the windowClosing.
All the suggestions I've seen on the net involve explicitly exiting in the windowClosing method, thus overriding the use of JFrame.setDefaultCloseOperation(), and duplicating the code in JFrame.processWindowEvent().
I actually have a dirty solution, but would like to see if there are any cleaner ones.
Cheers
-
Duncan McGregor over 13 yearsBut then my listener would need to know what the desired operation was. I suppose I could pass it the default close operation and duplicate the code in processWindowEvent, but it seems a shame.
-
Thirler over 13 yearsIf you mean the desired operation by the user, then yes. In the listener you would have to pop up the dialog. So if I understand what you want you correctly: you should not ask what the user wants until you get the close event.
-
Duncan McGregor over 13 yearsIndeed - I've edited the question to make this clearer I hope.
-
Thirler over 13 yearsActually my answer still applies then. The only change needed to your given example is that you need to call
setDefaultCloseOperation
on creation to prevent it from doing anything. And instead of canceling the close you explicitly close the window when you think it is required. -
Duncan McGregor over 13 yearsThe reason I don't like this is because it duplicates the code in JFrame, and requires the frame subclass to know what the required operation is when it creates the window listener. I suppose I could override setDefaultCloseOperation to pass the operation down to the listener...
-
Thirler over 13 yearsI will edit my answer with the solution I mean. In general you are the one that makes the JFrame, so you know which close operation you want, in essence you disable the close operation.
-
Duncan McGregor over 13 yearsThanks, but I understand how the stuff works (I've been programming in Swing since import com.sun.java.swing.*). I'm trying to find an elegant way of solving the problem.
-
Duncan McGregor over 13 yearsReading your answer I do like "The whole purpose of JFrame.setDefaultCloseOperation is only to prevent the need to implement WindowListeners for the most simple actions." This would be a lot easier to express if JFrame processWindowEvent delegated to a processCloseOperation that I could call.
-
Duncan McGregor over 13 yearsI think that's where we differ. I want a JFrame that is able to veto closes if the user says that they don't want to close it, but where the default close operation is a policy of the application, not the frame.
-
camickr over 13 yearsI thought I gave an elegant solution. It manages "prompts" and the "close operation" for you. I guess I don't understand what your concern is.
-
Duncan McGregor over 13 yearsI have to confess that I didn't click through to your code. Now that I have - ExitAction doesn't actually exit, it closes the active frame, which may or may not exit the app.
-
Duncan McGregor over 13 yearsAnd I can't decide if I like CloseListener manipulating defaultCloseOperation on the fly. It feels wrong to me, but I can see that it does the job. So thanks, I'll try to be a little less ungracious ;-)
-
Duncan McGregor over 13 yearsI've accepted this answer to give Thirler the credit, and so that the question doesn't remain open. Personally I'm staying with my ugly exception hack, you can take your pick of the 2.