Previous | Next | Trail Map | Creating a User Interface | 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(in the API reference documentation)class. It provides a useful subclass, FileDialog,(in the API reference documentation)that provides save and open dialogs.

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 iconified, its dependent dialogs disappear from the screen. When the window is deiconified, 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 current release, you can't create custom Dialog subclasses that are modal. [This should be fixed in the next bugfix 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 deals with the Dialog object:

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

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

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

        resize(350, 125);
    }

    public boolean action(Event event, Object arg) {
        if ( (event.target == setButton)
           | (event.target instanceof TextField)) {
            parent.setText(field.getText());
        }
        field.selectAll();
        hide();
        return true;
    }
}
Besides the methods used in the 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.


Previous | Next | Trail Map | Creating a User Interface | Using Components, the GUI Building Blocks