Concepts / Creating a JFrame Window

Creating a JFrame Window

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

  • Programming

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?

  • Only the window disappears and the program keeps running

  • The window is hidden but can be shown again

  • The whole program ends

Reveal answer
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.

java
Notes on the code
  • new JFrame("Drawing 1") creates the window object with a title
  • setDefaultCloseOperation is called on the window object and passes one of the JFrame constants
  • setSize and setVisible(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.

Close box clickedClose operation seton windowEXIT_ON_CLOSE:program endsDO_NOTHING_ON_CLOSE:window stays openHIDE_ON_CLOSE: windowhiddenDISPOSE_ON_CLOSE:window disposedtriggersEXIT_ON_CLOSEDO_NOTHING_ON_CLOSEHIDE_ON_CLOSEDISPOSE_ON_CLOSE
After the user clicks the close box, which step decides the outcome, and what does each setting lead to?

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.

EXIT_ON_CLOSEEnds the programDO_NOTHING_ON_CLOSEIgnores the attemptHIDE_ON_CLOSEHides the windowDISPOSE_ON_CLOSEReleases resourcespermanently
When the user clicks the close box, what does each of the four constants do to the window and to the running program?

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.

Visible windowHidden windowStill exists, can be shownagainDisposed windowResources released,finishedHIDE_ON_CLOSEsetVisible(true)DISPOSE_ON_CLOSE or dispose()
After the close box is clicked, does the window become hidden but alive, or disposed with its resources released?
OperationWindow afterwardCan be shown again?Program keeps running?
EXIT_ON_CLOSEProgram endsNoNo
DO_NOTHING_ON_CLOSEStays openNot applicable, still visibleYes
HIDE_ON_CLOSEHidden, still existsYes, with setVisible(true)Yes
DISPOSE_ON_CLOSEDisposed, resources releasedNoYes

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

Worked example

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?

  1. 1
    Identify the observed outcome

    The window vanished, but the program kept running. Both facts are the key clues.

  2. 2
    Match 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.

  3. 3
    Narrow 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.

  4. 4
    Locate the setting

    Search the code that creates the window for setDefaultCloseOperation, or for a call to dispose(). If neither appears, the default HIDE_ON_CLOSE applies.

Result

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 like

    A window is created with no close operation set, the user closes it, and the program keeps running.

    Why it fails

    HIDE_ON_CLOSE is the default when no close operation is specified. Only EXIT_ON_CLOSE ends the program.

    Do this instead

    Set 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 like

    A program hides a window with HIDE_ON_CLOSE, then later tries to reopen it.

    Why it fails

    A hidden window still exists and can be shown again with setVisible(true).

    Do this instead

    Use 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 like

    A window uses DISPOSE_ON_CLOSE and the learner expects the whole application to stop when it is closed.

    Why it fails

    DISPOSE_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 instead

    Choose EXIT_ON_CLOSE if closing this window should end the program.

  • Common mistake

    Placing the close decision in the wrong place

    Why it fails

    The source places the setDefaultCloseOperation call outside the window's constructor so that the surrounding program can choose the behavior.

    Do this instead

    Call setDefaultCloseOperation on the window object in the code that creates it, not hard-wired inside every window class.

Practice: Predict Before You Run

MEDIUM

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.
EASY

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 setDefaultCloseOperation on the window object.
  • If there is no such call, the default applies.

Summary of the Close Mechanism

  1. 1
    The default close operation, set with setDefaultCloseOperation on the window object, decides what happens when the user clicks a JFrame's close box.
  2. 2
    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.
  3. 3
    HIDE_ON_CLOSE is the default when no close operation is specified, and a hidden window can be shown again with setVisible(true).
  4. 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.
  5. 5
    When 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 a JFrame'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.