Methods of Dialogs

Since Dialog and Frame are both subclasses of Window, they share many methods including setLocation() and setSize(). The only methods that are significantly different between a dialog and a frame are the constructors. There are two constructors for Dialog which differ in whether or not the dialog is given a title. This titleless constructor is

Dialog d = new Dialog(new Frame(), false);
The second argument is a boolean which specifies whether or not the dialog should be modal. If it should be, pass true. If it shouldn't be, pass false. Modal dialogs are only modal relative to their parent frame, which is passed as the first argument to the constructor. That is they block input to the parent frame but not to other frames.

Can you pass null instead of a new Frame?

If you create the parent frame one right inside the constructor as done here, the dialog cannot be truly modal.

There are also some common differences between most frames and most dialogs, but these are not written in stone:

  1. Most Frames can be moved and resized by the user. Most Dialogs cannot be.
  2. Most Frames have title bars. Most Dialogs do not.
You can make a dialog resizable and movable by calling its setResizable() method with a boolean value of true like this:

d.setResizable(true);
You can give a dialog a title bar by adding the title string to the constructor:

Dialog d = new Dialog(new Frame(), "My Dialog Window", false);
All the other methods of the Dialog class are exactly the same as they are for Frames. You resize them the same way. You move them the same way. You make them visible the same way. You add components to them the same way.


Previous | Next | Top
Last Modified December 8, 1997
Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu