Custom Dialogs
The ScribbleApp
program of Example 11-15
displayed two kinds of dialogs: a confirmation dialog created by
JOptionPane
and a color selection
dialog created by JColorChooser
.
These Swing components support many basic dialog box needs. The
JOptionPane
class makes it easy to
display simple (and not-so-simple) information, confirmation, and
selection dialogs, while JColorChooser
and JFileChooser
provide color and file
selection capabilities. Most nontrivial applications, however, need to
create custom dialogs that go beyond these standard components. This
is easy to do with the JDialog
component.
Example
11-17 shows the FontChooser
class. It subclasses JDialog
and
uses the ItemChooser
class
developed in Example 11-14
to display font families, styles, and sizes to the user. A FontChooser
dialog is pictured in Figure 11-14. The inner class
FontChooser.Demo
is a simple
demonstration application you can use to experiment with the FontChooser
dialog.
Figure 11-14. The FontChooser dialog
JDialog
is a
RootPaneContainer
, like JFrame
, which means you can’t add children
to it directly. You must instead add them to the container returned by
getContentPane( )
. FontChooser
creates a modal dialog, which
means that the show( )
method
blocks and does not return to the caller until the user dismisses the
dialog. Finally, FontChooser
is
implemented as a subclass of JDialog
, so ...
Get Java Examples in a Nutshell, 3rd Edition 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.