A Dialog window is an independent subwindow meant to carry temporary notice apart from the main Swing Application Window. Most Dialogs present an error message or warning to a user, but Dialogs can present images, directory trees, or just about anything compatible with the main Swing Application that manages them.For convenience, several Swing component classes can directly instantiate and display dialogs. To create simple, standard dialogs, you use the
ProgressMonitorclass can put up a dialog that shows the progress of an operation. Two other classes,
JFileChooser, also supply standard dialogs. To bring up a print dialog, you can use the Printing API. To create a custom dialog, use the
The code for simple dialogs can be minimal. For example, here is an informational dialog:
Here is the code that creates and shows it:
JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");
There are two most useful
showMessageDialogmethod displays a simple, one-button dialog. The
showOptionDialogmethod displays a customized dialog — it can display a variety of buttons with customized button text, and can contain a standard text message or a collection of components.
Here i have some examples, taken from
DialogDemo.java, of using
showOptionDialog, and the
JOptionPaneconstructor. For more example code, see
DialogDemo.javaand the other programs listed in Examples that Use Dialogs.
Displays a modal dialog with one button, which is labeled "OK" (or the localized equivalent). You can easily specify the message, icon, and title that the dialog displays. Here are some examples of using