Java Reference
In-Depth Information
FIGURE 12.7:
Example of a dialog box.
12.11 Creating Dialog Boxes
A dialog box is similar to a window (i.e., a
JFrame
). However, it cannot be minimized
or restored. It has a single close button in the top right-hand corner of the window. For
example, when you create a game, you may want to add an
About
dialog box; see Figure 12.7.
This can include your name, when the game was developed, copyright considerations, and
so on. A dialog box has a key property that distinguishes it from a regular window. It can
be either
modal
or
modeless
.
A
modal
dialog box prevents interaction with all other windows of the application.
A
modeless
dialog box allows us to interact with it and other windows of the application
at the same time.
A dialog box is created by creating an object of type
JDialog
. The constructor takes
as input a reference to the parent window, the title of the dialog box, and a Boolean value.
If the Boolean value is
true
, then the dialog is modal. Otherwise, the dialog is modeless.
Note that a reference to the parent window is needed in order to disable interaction with
the parent window when the dialog box is modal.
Below is a simple example of how to create an about dialog window. The
AboutFrame
class creates a menu bar that has a single menu and a single menu item. When the menu
item is selected, an about dialog box is created and displayed.
import
java .awt. event .
∗
;
import
javax . swing .
∗
;
public class
AboutProgram
{
public static void
main(String [] args)
{
AboutFrame f =
new
AboutFrame () ;