Note: If you're not sure what a dialog box is then have a read of What Is a Dialog Box?
A message box is a simple pop-up window that displays a message to the user and is dismissed with the click of a button. To avoid having to build your own dialog box from scratch, the JOptionPane class provides methods to make a variety of dialog boxes by simply feeding them some parameters to determine the appearance of the dialog box.
Note: If you're looking to get a specific input from a user then have a look at Building an Input Dialog Box. It still uses a method from the JOptionPane class but it's worth distinguishing it from a simple message box as the options the user can choose are more varied.
The JOptionPane Class
To create a simple message dialog box you have a choice of three
showMessageDialog- This method is the most basic. It allows you to set the message, a title for the dialog box, a message type (e.g., warning message, error message), and only has an OK button.
showConfirmDialog- Again, you can set the message, a title, a message type, but you can also choose between a selection of button combinations (e.g., YES/NO, OK/CANCEL). It returns an int representing which button was clicked.
showOptionDialog- This is the most flexible method. In addition to setting the message and title, you have the choice of how to customize the dialog box. You can pick a message type and button combination, or you can provide a specific icon and an
Objectarray for the button options. The
Objectarray gives the flexibility to make the button names from an String array or even give them all images using an Icon array. It returns an int representing the user's choice of button.
Using the showMessageDialog Method
To create a simple message box use the
JOptionPane.showMessageDialog(this, "This is the dialog title" ,"This is the dialog message", JOptionPane.PLAIN_MESSAGE);
This will create a dialog with no icon - just a message, a title and an OK button.
That sounds like a pretty plain message box and that's because I've passed the static field
JOptionPane.PLAIN_MESSAGE as the message type. The message type tries to give the dialog box a certain feel by determining the icon that will be used. A
PLAIN_MESSAGE has no icon but the other fields do:
JOptionPane.ERROR_MESSAGE uses a red octagon with an exclamation mark in the middle to try and give the feel that something unexpected has happened.
Have another look at the first parameter in the above example. I'm using
this to refer to the
JFrame object as the parent of the dialog box. The first parameter of any of the three
JOptionPane methods is always the parent component (e.g.,
JPanel) of the dialog box. This allow the dialog box to determine its position on the screen relative to its parent. This parameter can be null if you want to create a dialog box without a container like a
The final thing to note about the
JOptionPane.showMessageDialog method is it does not return the user's response. There is only an OK button for them to click. As their options are so limited it's not worth trying to capture a response.
Read more in Creating a Message Dialog Box - Part II to see how the other JOptionPane methods are used.