Imagine you are in the position where your program needs to stop in its tracks and prompt the user for some reason - perhaps to tell them about an unforeseen error like a missing file, or asking for confirmation when removing a file. You can probably achieve this with the techniques and components you've learnt about so far - create a JFrame, put a message and some option buttons in it, disable all other components until the user has dealt with the options, etc. Or alternatively you could use the class Swing provides, JOptionPane, which does all of the above and more, usually in a single statement.
JOptionPane dialogs can be split into 4 distinct types :
Each dialog will block the program's execution until the required response is received.
JOptionPane contains a number of static methods allowing you to create dialog boxes very easily. Without further ado, let's look at a few you might use.
Probably the simplest type of pop-up dialog there is - the dialog contains a message and an OK button. Creating one is as easy as it sounds :
public static void showMessageDialog(Component comp, Object msg);where c is the component the dialog should appear in the centre of (or 'null' to make it appear in the middle of the screen), and msg is the message to display. So :
JOptionPane.showMessageDialog(null, "Hello World");
will produce a bog-standard message window in the centre of the screen, with the message "Hello World", the title "Message", and an OK button which must be clicked before the program will continue to execute.
As with the other dialog types, this method is overloaded to allow the programmer more control over how the dialog box will look. For example :
public static void showMessageDialog(Component comp, Object msg, String title, int messageType);
where the first two parameters are as before, the third is the text to appear in
the title bar, and the fourth is the type of message (which determines the type of icon that appears in the dialog). So :JOptionPane.showMessageDialog(null, "This is a boring message",
"Nothing to see", JOptionPane.WARNING_MESSAGE);will produce a dialog box, with a boring message, "Nothing to see" in the title bar
and a red warning triangle icon, as the last parameter specifies this message is a warning. Other types are ERROR_MESSAGE, INFORMATION_MESSAGE, QUESTION_MESSAGE and the rather boring PLAIN_MESSAGE (which results in no icon).Finally, there is another method with 5 parameters, the last one specifying a different
icon to display in the dialog :public static void showMessageDialog(Component comp, Object msg, String title, int messageType, Icon icon);
So :
JOptionPane.showMessageDialog(null, "This is a boring message", "Nothing to see", JOptionPane.WARNING_MESSAGE, new ImageIcon("foo.jpg"));
will look as before, but with 'foo.jpg' displayed instead of the warning triangle.
These are virtually the same as the showMessageDialog() methods above, but with one
vital difference. Whereas the message dialogs only have a single JButton to click, the confirmation dialogs have two or three (depending on whether the programmer wants OK/CANCEL, YES/NO, or YES/NO/CANCEL). This means different code can be executed for each response.There are 4 methods available for the confirmation dialogs, with 2, 4, 5 or 6 parameters.
public static int showConfirmDialog(Component comp, Object msg);
displays the given message, with the title bar containing "Select an Option", and the
available options are Yes, No and Cancel.int r =
JOptionPane.showConfirmDialog(null, "Click one");
if (r == JOptionPane.YES_OPTION)
System.out.println("You
chose yes!");
will do the obvious, and an integer value representing the button selected will be stored in the variable r. This value can be one of YES_OPTION, NO_OPTION, OK_OPTION, CANCEL_OPTION and CLOSED_OPTION (when the 'X' in the corner of the dialog is used). The CLOSED_OPTION constant evaluates to -1, so the 'X' button needs to be handled carefully, especially when the return value is used as an array index subsequently.
public static int showConfirmDialog(Component comp, Object msg, String title, int optionType);
This is almost identical to the 4-parameter showMessageDialog() method, but the last parameter specifies which option buttons appear (OK/CANCEL, YES/NO or YES/NO/CANCEL).
int r = JOptionPane.showConfirmDialog(null, "Click one", "Choose", JOptionPane.OK_CANCEL_OPTION);
displays the same dialog as before but with OK and CANCEL buttons.
public static int showConfirmDialog(Component comp, Object msg, String title, int optionType, int messageType);
is the same as the 4-parameter version, but the fifth argument determines the message type, and thus the pre-defined icon that is displayed. And :
public static int showConfirmDialog(Component comp, Object msg, String msg, int optionType, int messageType, Icon icon);
is the same as the 5-parameter method but with the specified icon displayed instead.
Most showInputDialog() methods each produce a dialog with a text field for input from
the keyboard. There is also a method for selecting from a combo box. This is useful for accepting input when it could be any one of numerous (or even infinite) options, for example someone's name or age. The showInputDialog() methods return the String input when a text field is supplied, or the Object selected when using a combo box (this is because each item in a combo box list is stored as an Object, but in practice the return value can be treated as a String, as a String is a subclass of Object).There are two methods with 2 parameters :
public static String showInputDialog(Object msg, Object initVal);
where msg is the message the user sees, and initialValue the text that will appear
in the text field to start with. The component the dialog appears above is assumed to be null, so the dialog appears in the centre of the screen.String s =
JOptionPane.showInputDialog("Enter some text", "foo");
System.out.println("You entered " + s);
displays the message and a text field with an initial text string of "foo". The String contained in the text field when the dialog is disposed of is returned to variable s.
public static String showInputDialog(Component comp, Object msg);
where comp is the component to be shown above, and msg is the message the user sees. If comp is null, this is logically equivalent to the previous method, but without an initial value in the text field. There is also a method provided which performs the same function as this one, but allows you to specify an initial value in addition :
public static String showInputDialog(Component comp, Object msg, Object initVal);
It is also possible, as with the other dialog types, to specify the message type :
public static String showInputDialog(Component comp, Object msg, String title, int messageType);
where messageType is, for example, JOptionPane.INFORMATION_MESSAGE (the previous methods default to QUESTION_MESSAGE), and title is the title bar text (as before).
Finally, the method that produces a dialog with a combo box is a bit of a monster, with 7 parameters, but most of the parameters have already been seen.
public static Object showInputDialog(Component comp, Object msg, String title, int messageType, Icon icon, Object[] options, Object initVal);
The first 5 parameters behave as you would expect by now. The sixth is an array of options for the combo box, and the seventh specifies which is selected initially. If the options argument is null, the user can enter whatever they want, via a text field instead of a combo box.
As stated above, this method returns an Object, as the list items in a combo box are stored as Objects. This is not usually a problem, as we can cast the Object returned to pretty much whatever we like :
String options[] = { "Hugh",
"Pugh", "Barney McGrew", "Cuthbert",
"Dibble", "Grubb"};
String s = (String)JOptionPane.showInputDialog(null, "Please select an
option", "Select...",
JOptionPane.WARNING_MESSAGE, new
ImageIcon("foo.jpg"), options, options[0]);
System.out.println("You chose " + s);
This means that we can also return Icons, JLabels, in fact any class you can think of (as every class in Java is a subclass of Object) :
ImageIcon icons[] = { new ImageIcon("ford.jpg"),
new ImageIcon("ferrari.jpg"), new
ImageIcon("volvo.jpg"), new ImageIcon("citroen.jpg"));
ImageIcon icon = (ImageIcon)JOptionPane.showInputDialog(null, "Select a
manufacturer", "Car of your dreams",
JOptionPane.PLAIN_MESSAGE, null,
icons, icons[0]);
label.setIcon(icon); // or something similar
This fact is illustrated if you take a look at button6 in PopUpDemo.java. The icons are displayed in an option dialog rather than an input dialog, but the principles are the same.
Option dialogs are the most complicated yet most powerful dialogs created by JOptionPane.
A message is displayed and options are available for selection, as in the complicated input dialog, but here each option will appear on the option dialog, by default in the form of a JButton.There is only one static method available to create an option dialog :
public static int showOptionDialog(Component comp, Object msg, String title,
int optType, int msgType, Icon icon, Object[] options, Object initVal);All arguments should be familiar by now, but here's an example anyway :
String names[] = { "Boyle", "Djemame",
"Fores", "Gillespie", "Jenkins", "Jimack"
};
int r = JOptionPane.showOptionDialog(null, "Pick your favourite",
By the way, the strange-looking last line is another way of saying "if r > -1, print
which name you selected, else inform the user that they closed the dialog". This demonstrates the ternary '?:' operator, where "x ? y : z" is logically equivalent to "if x evaluates to true, execute statement y, else execute statement z".