20.2. Using Dialogs

A dialog is a window that is displayed within the context of another window—its parent. You use dialogs to manage input that can't be handled conveniently through interaction with the view—selecting from a range of options, for example, or enabling data to be entered from the keyboard. You can also use dialogs for information messages or warnings. The JDialog class in the javax.swing package defines dialogs, and a JDialog object is a specialized sort of Window. A JDialog object will typically contain one or more components for displaying information or allowing data to be entered, plus buttons for selection of dialog options (including closing the dialog), so there's quite a bit of work involved in putting one together. However, for many of the typical dialogs that you will want to use, the JOptionPane class provides an easy shortcut to creating dialogs. Figure 20-2 shows a dialog that you'll create later in this chapter using just one statement.

Figure 20.2. Figure 20-2

You'll use this dialog to provide a response to clicking on a Help/About menu item that you'll add to Sketcher in a moment. First, though, you need to understand a little more about how dialogs work.

20.2.1. Modal and Non-Modal Dialogs

There are two different kinds of dialog that you can create, and they have distinct operating characteristics. You have a choice of creating either a modal ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th 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.