Creating a JFrame Window
The default close operation, set with setDefaultCloseOperation, decides what happens when the user clicks a JFrame's close box.
Predicting a Close Click
A graphical window built with JFrame has a close box in its corner. When the user clicks it, something happens, but the outcome is not the same for every window. The program decides what that click means. In this article you will watch a window respond to a close click, form a prediction about why it behaved that way, and then trace the cause back to a single setting. Whenever a window does not behave the way you expected, the same question applies: which close operation was set, and where was it set?
What do you think happens?
A single window has its close operation set to EXIT_ON_CLOSE. When the user clicks its close box, what happens to the program as a whole?
Reveal answer
The whole program ends.
EXIT_ON_CLOSE ends the program. The window's close box is the trigger, and the close operation is the setting that decides the outcome. Make your guess before reading the explanation that follows, because the rest of this article shows why the other three settings behave differently.
Where the Close Decision Is Made
The close operation is assigned by calling setDefaultCloseOperation on the window object, passing one of the JFrame constants. The call is not placed inside the window's constructor. The reason is flexibility: a window class that fixes its close behavior internally forces that choice on every program that uses it. Keeping the decision in the code that creates the window lets the surrounding program choose the behavior that fits its situation.
Consider a drawing application that lets the user open several drawing windows at once. If closing one window ended the entire program, the user could not close a single drawing without losing the others. The close operation for each window is therefore a choice made by the code that opens that window, not something hard-wired into every window.
- new
JFrame("Drawing 1")creates the window object with a title setDefaultCloseOperationis called on the window object and passes one of theJFrameconstantssetSizeandsetVisible(true)control the size and make the window appear on screen
Tracing a Single Close Click
The diagram below traces one close click through the four possible settings. Start at the click, follow the branch that matches the operation that was set, and read the outcome at the end of that branch. The question it answers is what the window and the program look like after the click.
The branch you follow depends entirely on the setting. The click is the same in every case; only the operation that was set differs. This is why two windows that look identical on screen can respond very differently when closed.
The Four Close Operations
A JFrame offers four constants for the default close operation. Each one answers the same question, namely what the window does when the user clicks its close box, but each gives a different answer. The four differ on two points: whether the program keeps running, and whether the window can come back. The comparison below summarizes the effect of each, and the subsections take them one at a time.
Hidden Versus Disposed Windows
Hiding and disposing both remove a window from the screen, so they look similar at first. The difference shows up in what you can do next. A hidden window still exists and can be shown again. A disposed window has given back its resources and is finished. The state diagram below follows a window through these states.
| Operation | Window afterward | Can be shown again? | Program keeps running? |
|---|---|---|---|
| EXIT_ON_CLOSE | Program ends | No | No |
| DO_NOTHING_ON_CLOSE | Stays open | Not applicable, still visible | Yes |
| HIDE_ON_CLOSE | Hidden, still exists | Yes, with setVisible(true) | Yes |
| DISPOSE_ON_CLOSE | Disposed, resources released | No | Yes |
Effect of each close operation on the window and the running program. The program column reflects the source's diagnostic guidance for a window that vanished while the program kept running.
Worked Example: A Window That Vanished
Diagnosing a surprise after a close click
A learner closes a window and sees it disappear, but the program keeps running in the background. Which setting most likely caused this, and where would you look to confirm it?
- 1Identify the observed outcome
The window vanished, but the program kept running. Both facts are the key clues.
- 2Match the outcome to the four settings
The source's diagnostic guidance says that a window that vanished while the program kept running points to HIDE_ON_CLOSE or DISPOSE_ON_CLOSE. EXIT_ON_CLOSE would have ended the program, and DO_NOTHING_ON_CLOSE would have kept the window on screen.
- 3Narrow the candidates
If the window was never configured, HIDE_ON_CLOSE is the default. If the program later tries to show the window again and it appears, HIDE_ON_CLOSE is consistent. If it cannot be shown again, DISPOSE_ON_CLOSE or a call to
dispose()is more likely. - 4Locate the setting
Search the code that creates the window for
setDefaultCloseOperation, or for a call todispose(). If neither appears, the default HIDE_ON_CLOSE applies.
The likely cause is HIDE_ON_CLOSE (the default when nothing is set) or DISPOSE_ON_CLOSE. The line that sets it, or its absence, is where the behavior diverges from what was expected.
Mistakes With Close Operations
Common mistake Assuming that closing a window always ends the program
What it looks likeA window is created with no close operation set, the user closes it, and the program keeps running.
Why it failsHIDE_ON_CLOSE is the default when no close operation is specified. Only EXIT_ON_CLOSE ends the program.
Do this insteadSet the close operation explicitly, and remember that the default hides the window rather than ending the program.
Common mistake Expecting a hidden window to be gone for good
What it looks likeA program hides a window with HIDE_ON_CLOSE, then later tries to reopen it.
Why it failsA hidden window still exists and can be shown again with
setVisible(true).Do this insteadUse setVisible(true) to bring the window back, or use DISPOSE_ON_CLOSE if the window should be finished when closed.
Common mistake Thinking DISPOSE_ON_CLOSE ends the program
What it looks likeA window uses DISPOSE_ON_CLOSE and the learner expects the whole application to stop when it is closed.
Why it failsDISPOSE_ON_CLOSE releases the window's resources permanently. It does not exit the program; EXIT_ON_CLOSE is the setting that ends it.
Do this insteadChoose EXIT_ON_CLOSE if closing this window should end the program.
Common mistake Placing the close decision in the wrong place
Why it failsThe source places the
setDefaultCloseOperationcall outside the window's constructor so that the surrounding program can choose the behavior.Do this insteadCall setDefaultCloseOperation on the window object in the code that creates it, not hard-wired inside every window class.
Practice: Predict Before You Run
A program creates two windows. Window A has setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) and Window B has no close operation set. The user clicks the close box on each window in turn. For each window, predict whether it can be shown again and whether the program keeps running, then check your reasoning against the comparison table.
Hints
- Window B has no close operation set, so which setting applies to it?
- Ask whether a disposed window can be made visible again.
- Neither window uses EXIT_ON_CLOSE, so consider what that tells you about whether the program ends.
Read a short program that creates a window and sets its close operation, and state which one of the four constants is in effect. Then explain, in one sentence, what the user will see after clicking the close box.
Hints
- Look for the call to
setDefaultCloseOperationon the window object. - If there is no such call, the default applies.
Summary of the Close Mechanism
- 1The default close operation, set with
setDefaultCloseOperationon the window object, decides what happens when the user clicks aJFrame's close box. - 2EXIT_ON_CLOSE ends the program, DO_NOTHING_ON_CLOSE ignores the attempt while still generating a
WindowEvent, HIDE_ON_CLOSE hides the window, and DISPOSE_ON_CLOSE releases its resources permanently. - 3HIDE_ON_CLOSE is the default when no close operation is specified, and a hidden window can be shown again with
setVisible(true). - 4
window.dispose()produces the same permanent close as DISPOSE_ON_CLOSE, but the program calls it rather than the user's click triggering it. - 5When a window behaves unexpectedly, check which close operation was set and where it was set.
At a glance
- The default close operation, set with
setDefaultCloseOperation, decides what happens when the user clicks aJFrame's close box. - EXIT_ON_CLOSE ends the program, DO_NOTHING_ON_CLOSE ignores the attempt while still generating a
WindowEvent, HIDE_ON_CLOSE hides the window, and DISPOSE_ON_CLOSE releases its resources permanently. - HIDE_ON_CLOSE is the default when no close operation is specified, and a hidden window can be shown again with
setVisible(true). window.dispose()performs the same permanent close as DISPOSE_ON_CLOSE, but the program calls it rather than the user.