Concepts / Standard Dialog Boxes in Java

Standard Dialog Boxes in Java

A custom dialog box is built as a subclass of JDialog, and it is a separate window much like a frame.

  • Programming

When Ready-Made Dialogs Are Not Enough

Most programs that need to ask the user something can rely on dialogs Java already provides. Earlier in the course you met three of them: JOptionPane, JColorChooser, and JFileChooser. The Mandelbrot Viewer program uses dialogs of all three kinds. These ready-made dialogs cover many common needs.

Sometimes, though, a program needs a window that asks a question or collects information in a shape the standard classes do not offer. For those cases Java lets you construct a custom dialog box of your own design. This article looks at what a custom dialog is built from, how its relationship to the window that opened it shapes its behavior, and the two behavioral categories a dialog can belong to.

What do you think happens?

A modal dialog is on the screen in front of your main window. Can the user still click buttons in the main window and keep working there?

  • Yes, the user can keep working in the main window

  • No, the rest of the application is blocked until the dialog is dismissed

Reveal answer
Answer:

No, the rest of the application is blocked until the dialog is dismissed

A modal dialog blocks the rest of the application until it has been dismissed. This is the central behavior to keep in mind as you read: when a dialog is open, what is the rest of the program allowed to do?

Building a Dialog as a JDialog Subclass

In Java, a custom dialog box is defined by writing a subclass of the JDialog class. Because the dialog is a subclass, the work of putting content into it and arranging that content happens in your own class, while JDialog supplies the general behavior of being a window. A dialog is therefore a separate window on the screen, in the same broad sense that a frame is a separate window.

The JDialog class is very similar to JFrame. If you already understand how a frame is shown and populated, most of that knowledge carries over. The difference that matters is not in how the window looks but in how it is attached to the rest of the program. That attachment is the subject of the next section.

Think of the custom dialog class as the place where your content lives, and JDialog as the base that makes it behave like a window. You write the content; JDialog provides the window behavior.

Every Dialog Has a Parent

Every dialog box has a parent. The parent is either a frame or another dialog box, and it is the window that owns the dialog. Ownership is what separates a dialog from an ordinary independent window. Two consequences follow directly from it.

Frameowns the first dialogDialog Aparent is the frameDialog Bparent is Dialog Aownsowns
How is each dialog linked to its parent, and what happens to the dialogs when a parent closes?

The first consequence concerns the lifetime of the dialog: it closes automatically when its parent closes. The second concerns the screen: a dialog may float on top of its parent even when the parent is the active window. The dialog stays visually in front of the window that owns it, so the relationship is visible to the user as well as enforced by the program. Together these effects make the dialog feel like a companion to its parent rather than a stranger that happens to be open at the same time.

Tracing a Modal Dialog Through the Program

A dialog box is either modal or modeless. Start with the modal case, since it is the one you will meet most often. A modal dialog, once it appears on screen, blocks the rest of the application. The application cannot be used in other ways until that dialog box has been dismissed. Read the sequence below from top to bottom to see the order in which the user, the main window, and the dialog interact.

Useracts in the programMain windowowns the dialogModal dialogblocks the application1. triggers an action2. shows the dialog3. other windows cannot be used while the dialog is open4. answers and dismisses the dialog5. program moves on
When a modal dialog is shown, what happens to the parent window, and in what order do the interactions occur?
Worked example

Following a confirmation request

A program's main window has a Delete button. Pressing it shows a modal dialog asking whether the user is sure. What is the state of the program at each stage?

  1. 1
    Before the button is pressed

    The main window is active and the user can click any control in it.

  2. 2
    The dialog appears

    Because the dialog is modal, the rest of the application is now blocked. Clicking the main window does nothing useful until the dialog is dismissed.

  3. 3
    The user answers

    The user chooses an answer and dismisses the dialog. The dialog's job of demanding an answer before the program moves on is complete.

  4. 4
    The program continues

    With the dialog gone, the application is no longer blocked and the program moves on based on the answer.

Result

The dialog held the program at the question until the user answered it, which is exactly the behavior of a modal dialog.

Modal and Modeless Dialogs Compared

The other category is the modeless dialog. A modeless dialog does not block the rest of the application. Because of that, it behaves more like an independent window than a question that must be answered first. It can remain on screen while the user works in other windows, and the user can switch back and forth freely.

Blocks theapplicationuntil dismissedDoes not blockrest of application staysusableDemands an answerfirstprogram waitsWorks alongsideongoing workuser switches freely
How does each kind of dialog differ in whether the user can still use the rest of the application?
QuestionModal dialogModeless dialog
Does it block the rest of the application?Yes, until it is dismissedNo
Can the user work in other windows while it is open?NoYes, and can switch back and forth freely
What does it ask of the user?Attention now, before the program moves onA window that can coexist with ongoing work
Is it owned by a parent?YesYes
Does it close when its parent closes?YesYes

Modal and modeless dialogs differ in blocking behavior, but share the same ownership relationship with their parent.

Key point

The ownership relationship applies to both categories. Choosing modal or modeless changes how the dialog interacts with the rest of the program, not whether it has a parent.

Choosing Between Modal and Modeless

The choice depends on what the program needs from the user at that moment. Ask whether the program can proceed without an answer. If it cannot, a modal dialog fits, because it holds the program until the answer arrives. If the window is something the user should keep open while doing other work, a modeless dialog fits better.

In the real world

Suppose a program needs the user to confirm a permanent deletion. The program should not continue until the user has answered, so a modal dialog suits this case. Now suppose a program offers a small window of search settings that the user adjusts while still working in the main document. That window should not block the document, so a modeless dialog suits this case.

Mistakes Beginners Make with Dialog Behavior

Dialog behavior is easy to misjudge because the windows look alike. The mistakes below come up often when learners first build custom dialogs.

  • Common mistake

    Assuming a custom dialog is modal by default

    Why it fails

    All standard Java dialogs are modal, but a custom dialog is modal only when you choose that behavior.

    Do this instead

    Decide explicitly whether the custom dialog should block the application, and set it up that way.

  • Common mistake

    Expecting a modeless dialog to block the main window

    Why it fails

    A modeless dialog does not block the rest of the application, so the user can keep working elsewhere.

    Do this instead

    Use a modeless dialog only when the user should be free to move between windows.

  • Common mistake

    Treating a dialog as an independent window with no owner

    Why it fails

    Every dialog has a parent, which is a frame or another dialog, and that parent owns it.

    Do this instead

    Think of the parent as part of the dialog's lifetime: the dialog closes automatically when the parent closes.

  • Common mistake

    Assuming the dialog's appearance changes its behavior

    Why it fails

    JDialog is very similar to JFrame. The important difference is how the dialog is attached to the program, not how it looks.

    Do this instead

    Reason about ownership and modality when deciding how a dialog should behave.

Predicting Dialog Behavior

EASY

A program opens a custom dialog from its main frame. The dialog is set up as modal. Before reading further, predict two things: whether the user can click the main frame while the dialog is open, and whether the dialog remains open if the main frame is closed. Then check your answer against the rules you have just read.

Hints
  • Recall which behavior is associated with modal dialogs.
  • Recall what a parent does to the dialogs it owns.

Key Takeaways

  • A custom dialog box is built as a subclass of JDialog, and it is a separate window much like a frame; JDialog supplies window behavior while your class supplies the content.
  • Every dialog has a parent, either a frame or another dialog. The dialog closes automatically when the parent closes and may float on top of it.
  • A modal dialog blocks the rest of the application until it is dismissed, and all standard Java dialogs are modal; a custom dialog is modal only when you choose that behavior.
  • A modeless dialog does not block the application and can stay on screen while the user works in other windows, but it still has a parent that owns it.
  • Choose modal when the program needs an answer before it can continue, and modeless when the window should coexist with ongoing work.