The JOptionPane class makes it easy for we to create and show standard modal dialogs.
It contains many static methods to create different kinds of JDialog, fill them with details, and show them as a modal JDialog.
When a JDialog is closed, the method returns a value to indicate the user's action on the JDialog.
JOptionPane class is used as a factory to create standard dialogs.
We can display the following four kinds of standard dialogs:
The static methods of the JOptionPane class to display a standard JDialog has name like showType
Dialog(). The Type
can be replaced with Message, Confirm, Input, and Option.
JOptionPane also have showInternalType
Dialog(),
which uses a JInternalFrame to display the dialog instead of a JDialog.
All four types of standard dialogs accept different types of arguments and return different types of values.
The following table shows the list of arguments of these methods and their descriptions.
ID | Argument/Description |
---|---|
1 | Component parentComponent The JDialog is centered on the parent component. If it is null, the JDialog is centered on the screen. |
2 | Object message Typically, it is a string. It can also display Swing component, Icon. If we pass any other object, the toString() method is called on that object and the returned string is displayed. We can also pass an array of objects and each element of the array will be displayed vertically one after another. |
3 | int messageType Set the type of the message. Depending on the type of message, a suitable icon is displayed in the dialog box. The available message types are:
|
4 | Int optionType Specify the buttons on the dialog box. We can use the following options.
|
5 | Object[] options Customize buttons on a dialog box. If we pass a Component object in the array, that component is displayed in the row of buttons. If we specify an Icon object, the icon is displayed in a JButton. Typically, we pass an array of strings as this argument to display a custom set of buttons in the dialog box. |
6 | String title title of the dialog box. A default title is supplied if we do not pass this argument. |
7 | Object initialValue Set the initial value that is displayed in the input dialog. |
Typically, when the user closes a dialog box, we want to check what button the user clicked. Here is the list of constants that we can use to check for equality with the retuned value:
The CLOSED_OPTION indicates that the user closed the dialog box using the close (X) menu button on the title bar.
OK_OPTION denotes that the user clicked the OK button on the dialog box to close it.
JOptionPane also lets we customize the labels for the buttons that it shows.
For more than one buttons in the dialog box, 0 is for the first button click, 1 for the second button click, and so on.
We can show a message dialog by using one of the showMessageDialog() static methods of the JOptionPane class.
A message dialog shows information with the OK button. The method does not return any value. Signatures of the showMessageDialog() methods are as shown:
showMessageDialog(Component parentComponent, Object message) showMessageDialog(Component parentComponent, Object message, String title, int messageType) showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)
The following code shows a message dialog.
import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Message!", "FYI", JOptionPane.INFORMATION_MESSAGE); } }
We can display a confirmation dialog box by using the showConfirmDialog() method.
The user's response is indicated by the return value.
The following code displays a confirmation dialog, and handles the user's response:
//from w ww .j a v a 2 s .c om import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { int response = JOptionPane.showConfirmDialog(null, "Save the file?", "Confirm Save Changes", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); switch (response) { case JOptionPane.YES_OPTION: System.out.println("yes"); break; case JOptionPane.NO_OPTION: System.out.println("no"); break; case JOptionPane.CANCEL_OPTION: System.out.println("cancel"); break; case JOptionPane.CLOSED_OPTION: System.out.println("closed."); break; default: System.out.println("something else"); } } }
We can ask the user for an input using the showInputDialog() method and specify an initial value for the user's input.
If we want the user to select a value from a list, we can pass an object array that contains the list. The UI will display the list in a suitable component such as a JComboBox or a JList.
The following code displays an input dialog.
import javax.swing.JOptionPane; /*from w w w. j av a 2 s . c om*/ public class Main { public static void main(String[] args) { String response = JOptionPane .showInputDialog("Please enter."); if (response == null) { System.out.println("cancelled."); } else { System.out.println("entered: " + response); } } }
The showInputDialog() method in the code above returns a String, which is the text the user enters in the input field.
If the user cancels the input dialog, it returns null.
The following code displays an input dialog with a list of choices. The user may select one of the choices from the list. This version of the showInputDialog() method returns an Object, not a String.
import javax.swing.Icon; import javax.swing.JComponent; import javax.swing.JOptionPane; // w w w. j a v a2s .co m public class Main { public static void main(String[] args) { JComponent parentComponent = null; Object message = "Please select"; String title = "JOptionPane Input Dialog"; int messageType = JOptionPane.INFORMATION_MESSAGE; Icon icon = null; Object[] selectionValues = new String[] { "A", "B", "C" }; Object initialSelectionValue = selectionValues[2]; Object response = JOptionPane.showInputDialog(parentComponent, message, title, messageType, icon, selectionValues, initialSelectionValue); if (response == null) { System.out.println("we have cancelled the input dialog."); } else { System.out.println("we entered: " + response); } } }
We can customize the option buttons using the showOptionDialog() method that is declared as follows:
int showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue)
The options parameter specifies the user's options.
If we pass components in the options parameter, the components are displayed as options. If we pass any other objects such as strings, a button is displayed for each element in the options array.
The following code shows how to display custom buttons in a dialog box.
import javax.swing.Icon; import javax.swing.JComponent; import javax.swing.JOptionPane; /* ww w .j a v a 2 s .c o m*/ public class Main { public static void main(String[] args) { JComponent parentComponent = null; Object message = "How is JOptionPane?"; String title = "JOptionPane Option Dialog"; int messageType = JOptionPane.INFORMATION_MESSAGE; Icon icon = null; Object[] options = new String[] { "A", "B", "C" }; Object initialOption = options[2]; int response = JOptionPane.showOptionDialog(null, message, title, JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, icon, options, initialOption); switch (response) { case 0: case 1: case 2: System.out.println("we selected:" + options[response]); break; case JOptionPane.CLOSED_OPTION: System.out.println("we closed the dialog box."); break; default: System.out.println("I don't know what we did."); } } }
By default, all dialog boxes from JOptionPane are not resizable. To customize them so that they are resizable, use the createDialog() methods of JOptionPane.
The following code displays the custom resizable dialog box.
import javax.swing.JDialog; import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { JOptionPane pane = new JOptionPane("JOptionPane", JOptionPane.INFORMATION_MESSAGE); String dialogTitle = "Resizable Custom Dialog"; JDialog dialog = pane.createDialog(dialogTitle); dialog.setResizable(true); dialog.setVisible(true); } }