Concepts / Creating Common Dialog Boxes with JColorChooser

Creating Common Dialog Boxes with JColorChooser

A dialog is a window used for short, single-purpose interactions, such as showing a message, asking a question, or letting the user pick a file or color.

  • Programming

The Interruption You Have Seen Before

Think about the last time a program interrupted your work to ask a question, show a warning, or let you pick a file. A small window appeared, did one job, and went away once you responded. Swing calls this kind of window a dialog, and it is one of the most common building blocks in graphical Java programs. Before looking at how dialogs are built, it helps to understand the job they do and how they relate to the windows around them.

Key point

A dialog is never a fully independent window. Every dialog is attached to another window, and the way it behaves depends on that attachment.

What a Dialog Is

Definition

A dialog is a window used for short, single-purpose interactions, such as showing a message, asking a question, or letting the user pick a file or color.

In Swing, a dialog is represented by a JDialog object or an object of a subclass of JDialog. A dialog is a separate window, much like a frame, so it has its own title bar and its own place on the screen. The difference is that a dialog exists to do one job and then get out of the way.

The kinds of tasks that belong in a dialog are short and focused: display a message the user must see, ask a question and collect an answer, or let the user choose something such as a file or a color. Tasks that involve the main work of a program, with many controls and long-lived content, belong in the main window instead.

Tracing the Parent Link

Every dialog has a parent window, which is either a frame or another dialog, and the dialog is dependent on that parent. The dependence is practical: if the parent is closed, the dialog closes with it. Follow the chain in the diagram below. The dialog hangs off its parent, and when the parent is removed, the dialog goes with it.

Parent frameowns the dialogDialogcloses when parent closesInvisible frameused when no parent isnamedDialog without anamed parentstill has a parentparent ofparent of
What does the dialog belong to, and what happens to it when the parent window is closed?

It is also possible to create a dialog without naming a parent. In that case, the system quietly creates an invisible frame and uses it as the parent. You never see this frame, but the dialog still has a parent relationship with it. The rule therefore holds in every case: a dialog always belongs to some window.

Modal Dialogs Hold the Conversation

A modal dialog is one that blocks its parent frame. When a modal dialog is created, the parent becomes unavailable for input. The user can look at the parent, but cannot click it, type into it, or otherwise act on it until the dialog is closed. The dialog effectively holds the conversation until the user finishes with it.

This waiting pattern is why modal dialogs suit questions the program needs answered before it can continue. From the user's point of view, nothing in the parent changes while the dialog is open. Once the dialog is dismissed, the parent becomes usable again.

Worked example

A save-changes question in a text editor

A text editor's main window is the parent of a dialog that asks whether unsaved changes should be saved before the document is closed. Decide whether this should be a modal or modeless dialog, and trace what the user can do while it is open.

  1. 1
    Identify the task

    The dialog asks a question that the program needs answered before it can continue. This is a short, single-purpose interaction, which is the kind of task a dialog suits.

  2. 2
    Choose the dialog kind

    Because the program must wait for the answer, a modal dialog fits. A modal dialog blocks its parent until it is closed, so the program's next step depends on the user's response.

  3. 3
    Trace the parent while the dialog is open

    The editor window cannot receive clicks or typing. The user can see the document but cannot edit it until the question is answered.

  4. 4
    Trace the parent link on close

    If the editor window were closed while the dialog was still open, the dialog would close with it, because every dialog depends on its parent.

Result

A modal dialog is the right choice here. The parent is blocked while the question is pending, and the dialog is tied to the editor window that owns it.

Modeless Dialogs Leave the Parent Open

A modeless dialog does not block its parent in the same way. Because the parent stays available, the dialog behaves more like an independent window that happens to be connected to a parent. The user can move between the two windows freely. The source notes that modeless dialogs are less common than modal ones, and that modal dialogs are easier to use.

Parent frameinput blockedParent frameinput availableDialogholds the conversationDialogindependent window
When a dialog is open, which windows can still receive input, and how does that differ between modal and modeless dialogs?

What do you think happens?

A modeless dialog is open, and the user closes its parent frame. What happens to the dialog?

Reveal answer
Answer:

The dialog closes along with the parent frame.

A modeless dialog still has a parent, and every dialog is dependent on its parent. Closing the parent closes the dialog too, even though the dialog did not block the parent while it was open.

Choosing Between Modal and Modeless

FeatureModal dialogModeless dialog
Effect on parentBlocks the parent frame until closedDoes not block the parent in the same way
Parent input while openUnavailableAvailable
User movementMust answer before returning to the parentCan move freely between windows
Typical useQuestions the program needs answered before it continuesIndependent windows connected to a parent
PrevalenceMore common and easier to useLess common
Parent link on closeCloses with the parentCloses with the parent

Modal and modeless dialogs differ in how they affect the parent window, but both remain attached to it.

Mistakes Beginners Make with Dialogs

Learners new to dialogs tend to make a few predictable errors. Each one comes from treating a dialog as if it were simply a smaller frame.

  • Common mistake

    Treating a dialog as an independent window that survives its parent

    What it looks like

    Assuming a dialog remains on screen after the frame that opened it is closed.

    Why it fails

    Every dialog depends on its parent, so closing the parent also closes the dialog.

    Do this instead

    Think of the dialog as attached to its parent for its entire life.

  • Common mistake

    Assuming a dialog with no named parent has no parent

    What it looks like

    Creating a dialog without naming a parent and assuming it floats free of any window.

    Why it fails

    The system creates an invisible frame and uses it as the parent, so the dialog still belongs to a window.

    Do this instead

    Remember that a dialog always belongs to some window, even when you do not name one.

  • Common mistake

    Expecting a modal dialog to leave the parent usable

    What it looks like

    Trying to click the main window while a modal question is still waiting for an answer.

    Why it fails

    A modal dialog blocks its parent frame, so the parent cannot receive input until the dialog is closed.

    Do this instead

    Plan the program so the parent is only interacted with after the modal dialog is dismissed.

  • Common mistake

    Choosing a modeless dialog for a question the program must answer first

    Why it fails

    A modeless dialog does not block the parent, so the program has no built-in pause waiting for the answer.

    Do this instead

    Use a modal dialog when the program needs the answer before it continues.

Practice Predicting Dialog Behavior

EASY

A settings program opens a modal dialog to ask the user to confirm a choice. While that dialog is open, the user tries to click the settings window behind it. Predict what happens, then explain how the answer would change if the dialog were modeless.

Hints
  • Recall what a modal dialog does to its parent frame.
  • Consider whether the parent stays available for input when the dialog is modeless.

Key Takeaways

  • A dialog is a separate window for short, single-purpose tasks such as showing a message, asking a question, or letting the user pick a file or color, and in Swing it is represented by a JDialog or a subclass of JDialog.
  • Every dialog has a parent window, either a frame or another dialog, and closing the parent also closes the dialog; if no parent is named, the system uses an invisible frame as the parent.
  • A modal dialog blocks its parent frame until it is closed, which suits questions the program must have answered before continuing.
  • A modeless dialog does not block its parent in the same way, behaving more like an independent window connected to its parent; modal dialogs are more common and easier to use.