Subpages: 1. Dialogs and choosers overview
2. Adding an "About" dialog
3. JOptionPane message dialogs
4. Customizing JColorChooser
5. Customizing JFileChooser
14.3 JOptionPane message dialogs
Message dialogs provided by the JOptionPane class can be used for many purposes in Swing apps: to post a message, ask a question, or get simple user input. The following example brings up several message boxes of different types with a common Shakespeare theme. Both internal and regular dialogs are constructed, demonstrating how to use the convenient showXXDialog() methods (see 14.1.2), as well as how to manually create a JOptionPane component and place it in a dialog or internal frame for display.
Each dialog is instantiated as needed and we perform no caching here (for purposes of demonstration). A more professional implementation might instantiate each dialog at startup and store them as variables for use throughout the application's lifetime.

Figure 14.6 JOptionPane with custom icon, message, and option button strings in a JDialog.
<<file figure14-6.gif>>

Figure 14.7 JOptionPane with custom icon and message in a JInternalFrame.
<<file figure14-7.gif>>

Figure 14.8 JOptionPane ERROR_MESSAGE message dialog with multi-line message.
<<file figure14-8.gif>>

Figure 14.9 JOptionPane INFORMATION_MESSAGE input dialog with custom icon, message, text field input, and initial selection.
<<file figure14-9.gif>>

Figure 14.10 JOptionPane INFORMATION_MESSAGE input dialog with custom icon, message, combo box input, and initial selection.
<<file figure14-10.gif>>

Figure 14.11 JOptionPane YES_NO_OPTION confirm dialog.
<<file figure14-11.gif>>
The Code: DialogBoxes.java
see \Chapter14\2
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DialogBoxes extends JFrame
{
static final String BOX_TITLE = "Shakespeare Boxes";
public DialogBoxes() {
super(BOX_TITLE);
setSize(400,300);
setLayeredPane(new JDesktopPane());
JMenuBar menuBar = createMenuBar();
setJMenuBar(menuBar);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu mFile = new JMenu("File");
mFile.setMnemonic('f');
JMenuItem mItem = new JMenuItem("Ask Question");
mItem.setMnemonic('q');
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane pane = new JOptionPane(
"To be or not to be ?\nThat is the question.");
pane.setIcon(new ImageIcon("Hamlet.gif"));
Object[] options =
new String[] {"To be", "Not to be"};
pane.setOptions(options);
JDialog dialog = pane.createDialog(
DialogBoxes.this, BOX_TITLE);
dialog.show();
Object obj = pane.getValue();
int result = -1;
for (int k=0; k<options.length; k++)
if (options[k].equals(obj))
result = k;
System.out.println("User's choice: "+result);
}
};
mItem.addActionListener(lst);
mFile.add(mItem);
mItem = new JMenuItem("nfo Message");
mItem.setMnemonic('i');
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String message = "William Shakespeare was born\n"+
"on April 23, 1564 in\n"
+"Stratford-on-Avon near London";
JOptionPane pane = new JOptionPane(message);
pane.setIcon(new ImageIcon("Shakespeare.gif"));
JInternalFrame frame = pane.createInternalFrame(
(DialogBoxes.this).getLayeredPane(), BOX_TITLE);
getLayeredPane().add(frame);
}
};
mItem.addActionListener(lst);
mFile.add(mItem);
mItem = new JMenuItem("Error Message");
mItem.setMnemonic('e');
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String message = "\"The Comedy of Errors\"\n"+
"s considered by many scholars to be\n"+
"the first play Shakespeare wrote";
JOptionPane.showMessageDialog(
DialogBoxes.this, message,
BOX_TITLE, JOptionPane.ERROR_MESSAGE);
}
};
mItem.addActionListener(lst);
mFile.add(mItem);
mFile.addSeparator();
mItem = new JMenuItem("Text Input");
mItem.setMnemonic('t');
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String input = (String) JOptionPane.showInputDialog(
DialogBoxes.this,
"Please enter your favorite Shakespeare play",
BOX_TITLE, JOptionPane.INFORMATION_MESSAGE,
new ImageIcon("Plays.jpg"), null,
"Romeo and Juliet");
System.out.println("User's input: "+input);
}
};
mItem.addActionListener(lst);
mFile.add(mItem);
mItem = new JMenuItem("Combobox Input");
mItem.setMnemonic('c');
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] plays = new String[] {
"Hamlet", "King Lear", "Otello", "Romeo and Juliet" };
String input = (String) JOptionPane.showInputDialog(
DialogBoxes.this,
"Please select your favorite Shakespeare play",
BOX_TITLE, JOptionPane.INFORMATION_MESSAGE,
new ImageIcon("Books.gif"), plays,
"Romeo and Juliet");
System.out.println("User's input: "+input);
}
};
mItem.addActionListener(lst);
mFile.add(mItem);
mFile.addSeparator();
mItem = new JMenuItem("Exit");
mItem.setMnemonic('x');
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (JOptionPane.showConfirmDialog(
DialogBoxes.this,
"Do you want to quit this application ?",
BOX_TITLE, JOptionPane.YES_NO_OPTION)
== JOptionPane.YES_OPTION)
System.exit(0);
}
};
mItem.addActionListener(lst);
mFile.add(mItem);
menuBar.add(mFile);
return menuBar;
}
public static void main(String argv[]) {
new DialogBoxes();
}
}
Understanding the Code



RSS feed Java FAQ News