Java Frames and Windows
A custom dialog box is built as a subclass of JDialog, and it is a separate window much like a frame.
Opening Question: What Can the Program Do While a Dialog Is Open?
Java programs often open a small window that asks a question, shows a message, or collects a piece of information. These windows are called dialog boxes. Java ships with ready-made dialogs through the JOptionPane, JColorChooser, and JFileChooser classes, and you have already met them earlier in the course. The Mandelbrot Viewer program uses dialogs of all three kinds.
Ready-made dialogs cover many common needs, but sometimes a program needs a window that asks a question or gathers information in a shape the standard classes do not offer. For those cases Java lets you build a custom dialog box of your own design. This article keeps one question in mind throughout: when a dialog is open, what is the rest of the program allowed to do?
What do you think happens?
Before reading on, what do you think happens to the main window of a program while a dialog box is open on screen?
Reveal answer
The rest of the application is blocked until the dialog is dismissed.
That is the defining behavior of a modal dialog. A modal dialog, once it appears on screen, blocks the rest of the application, and the application cannot be used in other ways until the dialog has been dismissed. Not every dialog behaves this way, which is why the rest of this article matters.
What a Custom Dialog Is Built From
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.
Because of this structure, a dialog is 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 to a dialog. The difference that matters is not in how the window looks. It lies in how the window is attached to the rest of the program.
Think of the JDialog superclass as supplying the machinery of a window, and your subclass as supplying the specific contents. The subclass is where you decide what the dialog shows and how its parts are arranged.
Why 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. The diagram below shows how a custom dialog sits in relation to its parent frame and to the JDialog class it extends.
Ownership has two consequences. First, the dialog closes automatically when its parent closes. Second, the 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. 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 on Screen
A modal dialog is the common case, and all of the standard dialog boxes in Java are modal. This is why they feel direct: each one demands an answer before the program moves on. A custom dialog is modal when you choose that behavior, and the same blocking rule applies to it. The sequence below traces a user, a main window, and a modal dialog over time.
A Settings Dialog That Must Be Answered
A text editor's main window has a Save As action. The developer builds a custom modal dialog that asks for a file name and a format. While this dialog is open, the user clicks inside the main editing area. What happens, and why?
- 1Identify the dialog type
The dialog is modal, which is the behavior chosen when the custom dialog is built. A modal dialog blocks the rest of the application.
- 2Check what the blocking covers
The application cannot be used in other ways until the dialog has been dismissed. The editing area belongs to the same application, so it is part of what is blocked.
- 3Trace the click
The click does not get the editing area to respond. The user must first answer the file name and format dialog and dismiss it.
- 4Check the ownership
The dialog's parent is the main window. If the main window were closed while the dialog was open, the dialog would close automatically too.
The click in the editing area has no effect while the modal dialog is on screen. The user must dismiss the dialog before the editor can be used again.
Modal and Modeless Dialogs Side by Side
A dialog box is either modal or modeless. The other category, the modeless dialog, does not block the rest of the application. 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. The diagram below contrasts the two categories.
| Feature | Modal dialog | Modeless dialog |
|---|---|---|
| Blocks the rest of the application | Yes, until dismissed | No |
| User can work in other windows while it is open | No | Yes |
| Typical role | Demands an answer before the program moves on | Behaves like an independent window alongside ongoing work |
| Standard Java dialogs | All of them are modal | Not a standard Java dialog type |
| Parent ownership | Applies | Still applies: the parent owns it and closes it when the parent closes |
Comparing the two behavioral categories of a dialog box
Notice what each category asks of the user. A modal dialog asks for attention now and holds the program until it gets it. A modeless dialog offers a window that can coexist with ongoing work. The ownership relationship from the previous section still applies to a modeless dialog: its parent still owns it, and it still closes when that parent closes.
Choosing Between Modal and Modeless
The choice depends on what the dialog asks of the user. The source describes the two behaviors clearly, so the guidance below follows directly from them. The questions are a way of applying those descriptions to a concrete task; they are not a fixed rule set.
- 1Ask whether the program must have an answer before it can continue correctly. If yes, a modal dialog matches that need, because it holds the program until the user responds.
- 2Ask whether the user should be free to consult or change other windows while the dialog stays open. If yes, a modeless dialog matches that need, because it can coexist with ongoing work.
- 3Ask whether the window is a question or a companion tool. A question that demands an answer suggests modal; an ongoing tool window suggests modeless.
- 4Whichever you choose, remember that the dialog has a parent, and that the parent's closing will close the dialog.
Suppose a program needs the user to confirm before overwriting a file. The program cannot sensibly continue without that answer, so a modal dialog fits. Now suppose the program offers a tool window for searching text while the user edits a document. The user should be able to type in the document while the search window stays visible, so a modeless dialog fits.
Mistakes Beginners Make with Dialogs
Common mistake Assuming every dialog is modeless
Why it failsAll of the standard dialog boxes in Java are modal, and a custom dialog is modal when you choose that behavior. Modal is the common case.
Do this insteadDecide the behavior deliberately when you build a custom dialog, and remember that the standard dialogs block the application.
Common mistake Thinking a modeless dialog is independent of its parent
Why it failsOwnership applies to every dialog. A modeless dialog's parent still owns it and it still closes when that parent closes.
Do this insteadTreat the parent relationship as present in both categories and check it when you reason about closing behavior.
Common mistake Building a custom dialog for a simple question
Why it failsReady-made dialogs cover many common needs. A custom dialog is for cases where the standard classes do not offer the shape of window you need.
Do this insteadTry JOptionPane, JColorChooser, or JFileChooser first, and build a custom JDialog subclass only when those do not fit.
Common mistake Expecting a custom dialog to look or behave like a frame in every respect, when the important difference is how it is attached to its parent
Practice: Predict the Blocking Behavior
A program's main window has two buttons. Button A opens a modal custom dialog that asks for a user name. Button B opens a modeless custom dialog that shows a running progress count. While the modal dialog is open, what can the user do in the main window? While the modeless dialog is open, what can the user do? Then explain what happens to both dialogs if the main window is closed.
Hints
- Recall the defining rule for a modal dialog: it blocks the rest of the application until it is dismissed.
- A modeless dialog does not block the application, so the user can switch between windows.
- Ownership applies to both categories: the parent's closing closes the dialog.
Key Takeaways
- A custom dialog box is built as a subclass of
JDialog, which makes it a separate window much like a frame, while the subclass holds its own content and layout. - Every dialog has a parent, which is a frame or another dialog; the dialog closes 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 modeless dialog does not block the application and lets the user work in other windows, though it still has a parent that owns it.
- Choose modal when the program needs an answer before continuing, and modeless when the user should be free to work in other windows.