| Using Components, the GUI Building Blocks

## How to Use Dialogs

> The AWT provides support for dialogs -- windows that are dependent on other windows -- with the Dialog class. It provides a useful subclass, FileDialog,that provides dialogs to help the user open and save files.

> The one thing that distinguishes dialogs from regular windows (which are implemented with Frame objects) is that a dialog is dependent on some other window (a Frame). When that other window is destroyed, so are its dependent dialogs. When that other window is miniaturized -- made into an icon -- its dependent dialogs disappear from the screen. When the window is returned to its normal state, its dependent dialogs return to the screen. The AWT automatically provides this behavior to you.

> Because no API currently exists to let applets find the window they're running in, applets generally can't use dialogs. The exception is that applets that bring up their own windows (Frames) can have dialogs dependent on those windows. For this reason, the following applet consists of a button that brings up a window that brings up a dialog.

> > Your browser doesn't understand the <APPLET> tag. Here's a snapshot of the window (Frame) the button brings up:

> > And here's a snapshot of the dialog for the window:

> Dialogs can be modal. Modal dialogs require the user's attention, preventing the user from doing anything else in the dialog's application until the dialog has been dismissed. By default, dialogs are non-modal -- the user can keep them up and still work in other windows of the application.

> Note: Due to a bug in the 1.0 release, you can't create custom Dialog subclasses that are modal. This is fixed in the 1.0.2 release.

> Here's the code for the window that the above applet brings up. This code can be run as a standalone application or, with the help of the AppletButton class, as an applet (as above). Here's just the code that implements the Dialog object:

> >     class SimpleDialog extends Dialog {
> >         TextField field;
> >         DialogWindow parent;
> >         Button setButton;

> >         SimpleDialog(Frame dw, String title) {
> >             super(dw, title, false);
> >             parent = (DialogWindow)dw;

> >             ...//Create and add components, such as the set button.

> >             //Initialize this dialog to its preferred size.
> >             pack();
> >         }

> >         public boolean action(Event event, Object arg) {
> >             if ( (event.target == setButton)
> >                | (event.target instanceof TextField)) {
> >                 parent.setText(field.getText());
> >             }
> >             field.selectAll();
> >             hide();
> >             return true;
> >         }
> >     }

> The `pack()` method used in the SimpleDialog constructor above is a method defined by the Window class. (Remember, Dialog is a Window subclass.) The `pack()` method resizes the window so that all its contents are at or above their preferred or minimum sizes (depending on the window's layout manager). In general, using `pack()` is preferable to calling the `resize()` method on a window, since `pack()` leaves the window's layout manager in charge of the window's size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size.

> Here's the code that brings up the dialog:

> >     if (dialog == null) {
> >         dialog = new SimpleDialog(this, "A Simple Dialog");
> >     }
> >     dialog.show();

> Besides the methods used in the first code example above, the Dialog class also provides the following methods:

> `Dialog(Frame, boolean)`
>   Like the constructor used above, but doesn't set the title of the dialog window.
> `boolean isModal()`
>   Returns true if the dialog is modal.
> `String getTitle()`, `String setTitle(String)`
>   Gets or sets (respectively) the title of the dialog window.
> `boolean isResizable()`, `void setResizable(boolean)`
>   Finds out or sets (respectively) whether the size of the dialog window can change.

| Using Components, the GUI Building Blocks