Concepts / Handling Window Events

Handling Window Events

The default close operation, set with setDefaultCloseOperation, decides what happens when the user clicks a JFrame's close box.

  • Programming

Same Click, Different Outcomes

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 see a window respond to a close click, form a prediction about why, 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?

Suppose a single window has its close operation set to EXIT_ON_CLOSE. The user clicks the close box. What happens to the program as a whole?

  • Only that window disappears and the program keeps running

  • The window stays on screen and nothing else changes

  • The entire program stops

Reveal answer
Answer:

The entire program stops.

EXIT_ON_CLOSE ends the program. The source describes it as the constant that ends the program when the close box is clicked, which means the close click on this window affects the whole application, not just the window.

Tracing a Close Click Through the Settings

Think of the close click as an input that the window must answer. The answer depends entirely on the close operation stored on that window. The close operation is one of four JFrame constants, and each constant gives a different answer to the same question: what does the window do when the user clicks its close box?

Close box clickedWhich operation wasset?Program endsEXIT_ON_CLOSEWindow stays openDO_NOTHING_ON_CLOSEWindow hiddenHIDE_ON_CLOSE, programkeeps runningWindow disposedDISPOSE_ON_CLOSE, programkeeps runningEXIT_ON_CLOSEDO_NOTHING_ON_CLOSEHIDE_ON_CLOSEDISPOSE_ON_CLOSE
When the user clicks the close box, which path does the JFrame take depending on the operation that was set, and where does each path end?

Read the diagram from the top. The close click always reaches the same decision point, and the branch you follow is determined by the close operation that was set on the window. Each branch ends in a different terminal state: the program ends, the window stays open, the window is hidden while the program keeps running, or the window is disposed while the program keeps running. The diagram answers one question: what do the window and the program look like after the click?

Setting the Close Operation in Code

The close operation is assigned by calling setDefaultCloseOperation on the window object and passing one of the JFrame constants. In the example program the call is placed outside the window's constructor, in the code that creates and configures the window. The source gives the reason: flexibility. A window class that fixes its close behavior internally forces that choice on every program that uses it.

java
Notes on the code
  • The window object is created here, in the program that uses it, rather than inside a window class.
  • setDefaultCloseOperation is called on the window object and receives one of the JFrame constants.
  • setVisible(true) makes the window appear on screen.

The reason for keeping this decision in the creating code becomes clear with a hypothetical drawing application that opens 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. Because the close decision lives in the code that creates the window, the surrounding program can choose the behavior that fits its situation. A drawing application might choose a close operation for each window it opens, while a different program using the same window class could choose differently.

The Four Close Constants

A JFrame offers four constants for the default close operation. They differ on two points: whether the program keeps running, and whether the window can come back. The subsections below take them one at a time.

Comparing the Four Outcomes Side by Side

ConstantDoes the window close or remain?Hidden or disposed?Does the program keep running?Can the window come back?
EXIT_ON_CLOSEClosesProgram ends with itNo, the program endsNo
DO_NOTHING_ON_CLOSERemains openNeitherYesNot applicable, it never closed
HIDE_ON_CLOSERemoved from screenHiddenYesYes, with setVisible(true)
DISPOSE_ON_CLOSERemoved from screenDisposedYesNo, the window is finished

How the four JFrame close constants differ in effect

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. Trace the state of the window in each case. The diagram below follows a window through the close operations and the call that can show it again.

VisibleHiddenstill existsDisposedresources releasedHIDE_ON_CLOSEsetVisible(true)DISPOSE_ON_CLOSE or dispose()
After HIDE_ON_CLOSE or DISPOSE_ON_CLOSE runs, what state is the window in, and can it be shown again?

A hidden window is still in the middle of the lifecycle: calling setVisible(true) brings it back. A disposed window has no such path back in the source's description, because it has given back its resources and is finished. The diagram shows the difference as a branch with a return edge on one side and a terminal state on the other.

A program can also close a window on its own initiative. The source notes that the same permanent result as DISPOSE_ON_CLOSE is available by calling the instance method window.dispose(). The difference is who triggers it: the user clicking the close box in one case, and the program deciding to close in the other.

Mistakes With Close Operations

Most surprises with window closing come from assuming the wrong close operation is in effect. The items below are the patterns that cause the most confusion.

  • Common mistake

    Assuming the window closes means the program ends

    What it looks like

    A window is closed with the close box, and the program keeps running in the background.

    Why it fails

    Only EXIT_ON_CLOSE ends the program. HIDE_ON_CLOSE and DISPOSE_ON_CLOSE remove the window while the program keeps running.

    Do this instead

    Check which constant was set on that window before predicting what the program does.

  • Common mistake

    Expecting a window with DO_NOTHING_ON_CLOSE to close

    What it looks like

    The user clicks the close box, and the window is still visible afterward.

    Why it fails

    DO_NOTHING_ON_CLOSE ignores the close attempt. The window stays open, though a WindowEvent is still generated.

    Do this instead

    If the window must respond to the close attempt, handle the event rather than expecting the default behavior to close it.

  • Common mistake

    Treating a hidden window as gone for good

    What it looks like

    After HIDE_ON_CLOSE runs, the code never calls setVisible(true) again, and the user cannot get the window back.

    Why it fails

    A hidden window still exists and can be shown again with setVisible(true). Only the disposed state is finished.

    Do this instead

    Keep a reference to the window and call setVisible(true) when it should reappear.

  • Common mistake

    Forgetting the default when no close operation is set

    What it looks like

    A window is created and no close operation is specified, and the programmer assumes it ends the program.

    Why it fails

    HIDE_ON_CLOSE is the default when no close operation is specified, so the window is hidden rather than the program ending.

    Do this instead

    Set the close operation explicitly with setDefaultCloseOperation whenever the default behavior is not what you want.

  • Common mistake

    Putting the close decision inside a window class that fixes it

    What it looks like

    A window class sets EXIT_ON_CLOSE in its constructor, and a program that opens several such windows loses all of them when one is closed.

    Why it fails

    A window that fixes its close behavior internally forces that choice on every program that uses it.

    Do this instead

    Call setDefaultCloseOperation in the code that creates the window, so the surrounding program chooses the behavior.

Finding Where a Window Diverges

When a window does not behave the way you expected, follow the same steps each time: identify the observed outcome, match it to a branch of the close-box decision, and then find the line where the close operation was set.

MEDIUM

A program opens a settings window. When the user clicks its close box, the settings window disappears, but the main application keeps running. Later the program needs to show the settings window again, and it cannot. Which close operation is most likely set on the settings window, what would you expect the lifecycle to be, and where would you look to change the behavior?

Hints
  • Match the observed outcome, the window vanishing while the program keeps running, to a branch in the close-box decision.
  • Ask whether the window can be shown again after that close operation, using the lifecycle diagram.
  • The close operation is set with setDefaultCloseOperation on the window object, usually in the code that creates the window.

Key Takeaways

  • The default close operation, set with setDefaultCloseOperation, decides what happens when the user clicks a JFrame's close box.
  • EXIT_ON_CLOSE ends the program, DO_NOTHING_ON_CLOSE ignores the close attempt while still generating a WindowEvent, HIDE_ON_CLOSE hides the window so it can be shown again, 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() produces the same permanent close as DISPOSE_ON_CLOSE, but it is called by the program rather than triggered by the user.
  • When a window behaves unexpectedly, ask which close operation was set and where it was set.