Chapter 10. Swing Dialogs
In most
GUI applications, certain information needs to be
displayed for a brief period of time, often just long enough for the
user to read it and click “OK” or perhaps enter some
information, such as a username and password. In
AWT, such interactions were typically carried out
using the Dialog
class.
As you’d probably expect if you’ve already read Chapter 8, Swing extends Dialog
with
a class called JDialog
that implements the
RootPaneContainer
interface. This in itself is not
a particularly exciting enhancement, though it does give you the
ability to add a menu to a dialog box if you have some reason to do
so.
The much more interesting new Swing feature is the
JOptionPane
class. This class makes creating
simple dialog boxes extremely easy — in many cases requiring
just one line of code. We’ll look at both of these new classes
in this chapter.
The JDialog Class
JDialog
is the Swing replacement for its
superclass, java.awt.Dialog
. It provides the same
key changes described in Chapter 8,[22] in the
discussion of JWindow
, JFrame
,
and JApplet
— it uses a
JRootPane
as its container, and it provides
default window-closing behavior. Since JDialog
extends java.awt. Dialog
, it has a heavyweight
peer and is managed by the native windowing system. Figure 10.1 shows how JDialog
fits into
the class hierarchy.
Figure 10-1. The JDialog class hierarchy
Properties
JDialog
Get Java Swing now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.