Showing and Hiding Windows with setVisible
The default close operation, set with setDefaultCloseOperation, decides what happens when the user clicks a JFrame's close box.
A Window That Responds Differently
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, and trace the cause back to a single setting.
The setting in question is the default close operation, set with setDefaultCloseOperation. It tells a window how to react when the user closes it. 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?
Suppose a window's close operation is set to EXIT_ON_CLOSE and the program also has other windows open. When the user clicks that window's close box, what happens to the program as a whole?
Reveal answer
The program ends.
EXIT_ON_CLOSE ends the program. Because the program ends, the other open windows end with it. This is the most drastic of the four close operations, and it is the first thing to check when a whole application disappears after one window is closed.
Tracing a Single Close Click
The diagram below follows one close click through the four possible settings. Start at the close click, follow the branch that matches the operation that was set, and read the terminal state at the end of that branch. The diagram answers one question: what do the window and the program look like after the click?
Read the diagram as a diagnostic tool. If a window vanished but the program kept running, the setting was probably HIDE_ON_CLOSE or DISPOSE_ON_CLOSE. If the window refused to close at all, the setting was probably DO_NOTHING_ON_CLOSE. If the entire program stopped, EXIT_ON_CLOSE was set on that window. Each branch corresponds to one of the four constants you will study in the next section.
Where the Close Decision Is Made
The close operation is assigned by calling setDefaultCloseOperation on the window object and passing one of the JFrame constants. In the source example, this call sits outside the window's constructor. That placement is deliberate. A window class that fixes its close behavior internally forces that choice on every program that uses it.
Consider a hypothetical 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. Keeping the close decision in the code that creates the window lets the surrounding program choose the behavior that fits its situation.
- The window is created here, in the code that uses it.
- The close operation is set on the window object after creation, not inside a constructor.
setVisible(true)makes the window appear on screen.
The Four Close Operations
A JFrame offers four constants for the default close operation. Each 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.
| Constant | What happens on close | Program keeps running? | Can the window come back? |
|---|---|---|---|
| EXIT_ON_CLOSE | Ends the program | No | No |
| DO_NOTHING_ON_CLOSE | Ignores the attempt, but still generates a WindowEvent | Yes | Window stays open, so it did not leave |
| HIDE_ON_CLOSE | Hides the window | Yes | Yes, with setVisible(true) |
| DISPOSE_ON_CLOSE | Releases its resources permanently | Yes | No, the window is finished |
The four default close operations, compared on the two points the source highlights.
EXIT_ON_CLOSE is the strongest choice because it ends the program. DO_NOTHING_ON_CLOSE is unusual: the close attempt is ignored, but the window still generates a WindowEvent, so the program can still observe that the user tried to close it. HIDE_ON_CLOSE is the default when no close operation is specified. DISPOSE_ON_CLOSE releases the window's resources permanently.
Hidden Windows 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 with setVisible(true). A disposed window has given back its resources and is finished. The diagram below traces the state of the window in each case.
- HIDE_ON_CLOSE keeps the window object alive for later use.
setVisible(true)on a hidden window shows it again.- DISPOSE_ON_CLOSE finishes the window; it is not a way to hide it temporarily.
The program can also close a window on its own initiative. The same permanent result as DISPOSE_ON_CLOSE is available by calling the instance method window.dispose(). The difference is who triggers it: the close operation responds to the user's close box, while dispose() is called when the program decides.
Diagnosing a Surprise
A window that disappears but the program keeps running
A student's program creates a window. After the user clicks the close box, the window is gone, but the program is still running and the student expected the whole application to stop. Find the cause.
- 1Observe the symptom
The window vanished, so it was hidden or disposed, not left open.
- 2Rule out EXIT_ON_CLOSE
If EXIT_ON_CLOSE were set, the entire program would have ended. The program kept running, so this constant is not the cause.
- 3Rule out DO_NOTHING_ON_CLOSE
The window did not stay open, so the close attempt was not ignored.
- 4Identify the remaining candidates
HIDE_ON_CLOSE or DISPOSE_ON_CLOSE both remove the window from the screen while the program continues.
- 5Locate the setting
Search the code for the call to
setDefaultCloseOperationon this window object. Check whether it was set at all, since HIDE_ON_CLOSE is the default when no close operation is specified.
The window was hidden (HIDE_ON_CLOSE) or disposed (DISPOSE_ON_CLOSE). If the student wanted to bring it back, the fix is HIDE_ON_CLOSE with setVisible(true). If it was DISPOSE_ON_CLOSE, the window cannot be shown again and a new window must be created.
Mistakes Beginners Make
Common mistake Treating hiding and disposing as the same thing
Why it failsBoth remove the window from the screen, but a hidden window still exists and can be shown again, while a disposed window has given back its resources and is finished.
Do this insteadChoose HIDE_ON_CLOSE when the window should come back, and DISPOSE_ON_CLOSE when it should be finished.
Common mistake Expecting a window to return after DISPOSE_ON_CLOSE
What it looks likeframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); after the close click, calling frame.setVisible(true) does not bring back the finished window.
Why it failsDisposal releases the window's resources permanently.
Do this insteadCreate a new window object if a fresh window is needed.
Common mistake Assuming EXIT_ON_CLOSE only affects one window
Why it failsEXIT_ON_CLOSE ends the program, so the other windows end too.
Do this insteadUse EXIT_ON_CLOSE only for the window whose closing should end the whole program.
Common mistake Assuming DO_NOTHING_ON_CLOSE means nothing happens at all
Why it failsThe close attempt is ignored, but the window still generates a
WindowEvent.Do this insteadExpect the window to stay open, and be aware that the event is still generated.
Common mistake Forgetting that HIDE_ON_CLOSE is the default when no close operation is specified, then wondering why a window did not exit the program
Practice: Predict Before You Check
A program creates two JFrame windows. Window A has setDefaultCloseOperation set to HIDE_ON_CLOSE. Window B has setDefaultCloseOperation set to DISPOSE_ON_CLOSE. Before checking, predict what happens when the user clicks the close box on each window, and whether the program keeps running afterward. Then say how you would show Window A again, and whether Window B can be shown again.
Hints
- Use the flowchart: follow the branch that matches each window's constant.
- Ask whether the window still exists after the close click.
setVisible(true)works on a window that still exists; it cannot revive a finished one.
Key Takeaways
- 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. - A hidden window still exists and can be shown again with
setVisible(true); a disposed window is finished. window.dispose()gives the same permanent result as DISPOSE_ON_CLOSE, but the program calls it rather than the user.- When a window behaves unexpectedly, find which close operation was set and where it was set.