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 dealt with. 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.

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:

//[HOW DO I MAKE THIS GET THE FOCUS?]
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;
    }
}


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