
that asks a question with a confirm dialog (note the use of a string array for the
message argument):
int response = JOptionPane.showConfirmDialog(mainpanel, new String[] {
/* first line of the message */ "There are unsaved files.",
/* second line of message */ "Save them before quitting?"},
/* dialog title */ "Save Before Quitting?",
/* what buttons to display */ JOptionPane.YES_NO_CANCEL_OPTION,
/* icon type to display */ JOptionPane.WARNING_MESSAGE);
switch(response) {
case JOptionPane.YES_OPTION: saveAndQuit();
case JOptionPane.NO_OPTION: quitWithoutSaving();
case JOptionPane.CANCEL_OPTION:
case JOptionPane.CLOSED_OPTION: break; // Don't quit!
}
Input Dialogs
The showInputDialog() and showInternalInputDialog() methods are designed to
ask for input that is more complex than a yes-or-no answer. The simple versions
of
showInputDialog() support asking a question like “What is your name?” and
letting the user type a response in a text input area:
String name = JOptionPane.showInputDialog(frame, "What is your name?");
The more complex version of this method allows the user to select an object from
a list or pull-down menu of predefined options.
The arguments to
showInputDialog() ar e quite similar to those passed to showMes-
sageDialog() and showConfirmDialog().Todisplay a list of options to the user,
use the seven-argument version of the method and pass in an array of choices and
the default choice to ...