Java Reference
In-Depth Information
Clearly since the first parameter is of type
Frame
, you can supply a reference of type
Frame
or type
JFrame
. There are a further five constructors for creating
JDialog
objects with a
Dialog
or
JDialog
object as the parent. The only difference between these and the ones in the table overleaf is
that the type of the first parameter is
Dialog
rather than
Frame
. Any of these constructors can throw
an exception of type
HeadlessException
if the system on which the code is executing does not have
a display attached.
After you've created a
JDialog
object using any of the constructors, you can change the kind of dialog
window it will produce from modal to non-modal, or vice versa, by calling the
setModal()
method for the
object. If you specify the argument to the method as
true
, the dialog will be modal, and a
false
argument
will make it non-modal. You can also check whether a
JDialog
object is modal or not. The
isModal()
method for the object will return
true
if it represents a modal dialog, and
false
otherwise.
All
JDialog
objects are initially invisible, so to display them you must call the
setVisible()
method for the
JDialog
object with the argument
true
. This method is inherited from the
Component
class via the
Container
and
Window
classes. If you call
setVisible()
with the
argument
false
, the dialog window is removed from the screen. Once you've displayed a modal dialog
window, the user can't interact with any of the other application windows until you call
setVisible()
for the dialog object with the argument
false
, so you typically do this in the event handler which is
called to close the dialog. Note that the
setVisible()
method only affects the visibility of the dialog.
You still have a perfectly good
JDialog
object so that when you want to display the dialog again, you
just call its
setVisible()
method with an argument set to
true
. Of course, if you call
dispose()
for the
JDialog
object, or set the default close operation to
DISPOSE
_
ON
_
CLOSE
, then you won't be
able to use the
JDialog
object again.
To set or change the title bar for a dialog, you just pass a
String
object to the
setTitle()
method
for the
JDialog
object. If you want to know what the current title for a dialog is, you can call the
getTitle()
method which will return a
String
object containing the title bar string.
Dialog windows are resizable by default, so you can normally change the size of a dialog window by
dragging its boundaries. If you don't want to allow a dialog window to be resized, you can inhibit this
by calling the
setResizable()
for the
JDialog
object with the argument as
false
. An argument
value of
true
re-enables the resizing capability.
A Simple Modal Dialog
The simplest kind of dialog is one that just displays some information. We could see how this works by
adding a
Help
menu with an
About
menu item, and then displaying an
About
dialog to provide
information about the application.
Let's derive our own dialog class from
JDialog
so we can create an
About
dialog.
Try It Out - Defining the AboutDialog Class
The constructor for our
AboutDialog
class will need to accept three arguments - the parent
Frame
object, which will be the application window in Sketcher, a
String
object defining what should appear
on the title bar and a
String
object for the message we want to display. We'll only need one button in
the dialog window, an
OK
button to close the dialog. We can make the whole thing self-contained by
making the
AboutDialog
class the action listener for the button, and since it's only relevant in the
context of the
SketchFrame
class, we can define it as an inner class.