Dialogs are popup windows for communicating
with the user for a specific purpose such as:
- Warnings
- Messages
- Queries
- Selection of a directory and/or files, typically for saving
or opening a file.
A dialog window acts in two ways:
- Modal - becomes the top window and will not release control
to any other window until it is answered and closed.
- Nonmodal - can become a secondary window while waiting
to be answered and closed.
The Swing (javax.swing)
framework provides several ways to create a dialog:
- Create a subclass of JFrame
and add dialog functionality yourself such as a window
closing mechanism.
- Subclass JDialog,
which is a subclass of JFrame
and provides various capabilities such as modal/non-modal behavior.
- Use JOptionPane
and its various methods such as showConfirmDialog()
and showMessageDialog()
to generate basic dialogs with icons, buttons, message text, and
closing mechanism.
- Use JFileChooser
and JColorChooser
for selecting files/directories and colors, resp.
Generally, the JOptionPane approach is the much easier one for
basic dialogs and the two chooser classes are excellent for their
tasks. Create a JDialog
subclass when you need a more elaborate dialog with, for example,
checkboxes or other components that provide the user with multiple
options. JFrame
subclasses should generally be reserved for more complex tasks that
require a fully developed window.
In the following pages we provide demo programs to illustrate dialogs
with JOptionPane
References & Web Resources
|