Java: JOptionPane showMessageDialog examples (part 1)

[toc]

I’ve been working with the Java JOptionPane showMessageDialog a lot lately, so I thought I’d create a page here with a number of showMessageDialog examples, sort of a JOptionPane reference page.

I’ll walk you through some Java JOptionPane examples here, starting with a simple example and then increasing the level of difficulty as I go on.

A simple JOptionPane example

Starting with a simple example, if you just want to show a JOptionPane dialog with a simple text message, all you need is one line of Java source code, like this:

JOptionPane.showMessageDialog(frame, "A basic JOptionPane message dialog");

When this line of code is executed it will display the following message dialog:

JOptionPane showMessageDialog - simple example

In that example my first argument to the JOptionPane showMessageDialog method is a frame object, which presumably is an instance of a JFrame. If for some reason you don't have a reference to JFrame or JWindow instance, you can make that field null, and still display the identical JOptionPane dialog, as shown in this example:

JOptionPane.showMessageDialog(null, "A basic JOptionPane message dialog");

Note that when you supply a null argument like that, the JOptionPane dialog will be centered on the user’s screen. When you supply a JFrame reference, the dialog is centered on that JFrame, so this behavior can be slightly different.

A complete example

Now looking at this as a more complete showMessageDialog example, here is the source code for a complete Java class that demonstrates a showMessageDialog example with a slightly more complex message:

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ShowMessageDialogExample1
{
  public static void main(String[] args)
  {
    String backupDir = "/Users/al/backups";
    
    // create a jframe
    JFrame frame = new JFrame("JOptionPane showMessageDialog example");
    
    // show a joptionpane dialog using showMessageDialog
    JOptionPane.showMessageDialog(frame,
        "Problem writing to backup directory: '" + backupDir + "'.");
    System.exit(0);
  }
}

JOptionPane showMessageDialog - example with no title

JOptionPane dialog: A custom title

Taking this JOptionPane showMessageDialog example to the next level, in a real world application you want to display your message dialog with a title, so next I’ll add a title to the showMessageDialog method. However, adding a title also forces me to add a message type when I call the showMessageDialog method (as you'll see from the general syntax options available, shown later), so in this JOptionPane example I’ll choose the INFORMATION_MESSAGE type:

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ShowMessageDialogExample1
{
  public static void main(String[] args)
  {
    String backupDir = "/Users/al/backups";
    
    // create a jframe
    JFrame frame = new JFrame("JOptionPane showMessageDialog example");

    // show a joptionpane dialog using showMessageDialog
    JOptionPane.showMessageDialog(frame,
        "Problem writing to backup directory: '" + backupDir + "'.",
        "Backup problem",
        JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
  }
}

This results in the following message dialog:

JOptionPane showMessageDialog - custom title example

As you can see, this JOptionPane message dialog now includes a title, and also includes a new icon that wasn’t there before. This icon comes from the addition of the INFORMATION_MESSAGE argument to the showMessageDialog method.

JOptionPane with error message icon

I won’t repeat all that Java source code here, but if you repeat the exact same example, and replace the INFORMATION_MESSAGE type with an ERROR_MESSAGE type, like this:

JOptionPane.ERROR_MESSAGE

you'll see the following JOptionPanemessage dialog:

JOptionPane showMessageDialog - error message icon example

Again, just changing the JOptionPane message type changes the icon that is displayed, which in this case is the JOptionPane error icon.

JOptionPane warning message icon

You can also change the JOptionPane message to a WARNING_MESSAGE message type, like this:

JOptionPane.WARNING_MESSAGE

When you do so, the following dialog with a warning icon is displayed:

JOptionPane showMessageDialog - warning message icon example

JOptionPane with a plain message

You can also specify the JOptionPane PLAIN_MESSAGE type, like this:

JOptionPane.PLAIN_MESSAGE

This results in the JOptionPane removing the icon from the dialog that is displayed, so the resulting dialog will look like this:

JOptionPane showMessageDialog - plain_message example