Java Reference
In-Depth Information
ponents for displaying information or allowing data to be entered, plus buttons for selecting dialog options
(including closing the dialog), so there's quite a bit of work involved in putting one together. However, for
many of the typical dialogs that you might want to use, the JOptionPane class provides an easy shortcut to
creating dialogs. Figure 20-2 shows a dialog that you will create later in this chapter using just one state-
ment.
FIGURE 20-2
You use this dialog to provide a response to clicking on a Help/About menu item that you will add to
Sketcher in a moment. First, though, you need to understand a little more about how dialogs work.
Modal and Modeless Dialogs
There are two different kinds of dialog that you can create, and they have distinct operating characteristics.
You have a choice of creating either a modal dialog or a modeless dialog .
There are three varieties of modal dialog:
Application-modal dialogs: Prevent user interaction with all other windows in the same applic-
ation except for windows that are created by the dialog object. Interaction with other application
windows is restored when an application modal dialog closes.
Document-modal dialogs: Block all ancestor windows of the dialog window that share a com-
mon root. The windows in the hierarchy each have an owner except for the root window.
Toolkit-modal dialogs : Prevent interaction with all other windows that execute in the same
toolkit. When a web page has several applets running, they all share the same toolkit so a dialog
created by one applet can prevent interaction with other applets in the same web page.
A modeless dialog can be left on the screen for as long as you want, because it doesn't block interaction
with other windows in the application. You can also switch the focus back and forth between a modeless
dialog and any other application windows that are on the screen. I only discuss modeless and application-
modal dialogs in this topic. I'll refer to application-modal dialogs simply as modal dialogs.
You display a modal dialog typically by selecting a menu item or clicking a button. The dialog in Figure
20-2 that displays a message is a modal dialog. The dialog window retains the focus as long as it is dis-
played, and the application cannot continue until you click the OK button. Modal dialogs that manage input
normally have at least two buttons: an OK button, which you use to accept whatever input has been entered
and then close the dialog, and a Cancel button to just close the dialog and abort the entry of the data. Dialogs
that manage input are almost always modal dialogs, simply because you don't generally want to allow other
interactions to be triggered until your user's input is complete.
Whether you create a modal or a modeless dialog is determined either by an argument to a dialog class
constructor, or by which constructor you choose, because some constructors create modeless dialogs by de-
fault. The default, no-argument JDialog constructor creates a modeless dialog with an empty title bar. Be-
cause you have no provision for specifying a parent window for the dialog with the no-argument constructor,
a shared hidden frame will be assigned as the parent in this case. You have a choice of five constructors for
creating a JDialog object where the parent can be a window of type Frame or JFrame :
 
 
Search WWH ::




Custom Search