JOptionPane showMessageDialog examples (part 1)

JOptionPane showMessageDialog examples: 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 JOptionPane showMessageDialog examples, sort of a JOptionPane reference page.

I'll walk you through some JOptionPaneexamples here, starting simple, and increasing the level of difficulty as we go on.

Simple JOptionPane example

Starting with an easy JOptionPane 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");

Inside a running Java program, 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 showMessageDialogmethod is a frameobject (which presumably is an instance of a JFrame). If for some reason you don't have a reference to JFrameor 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");

A complete example

Now looking at this as a more complete JOptionPane 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 - 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_MESSAGEtype:

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_MESSAGEargument to the showMessageDialog method.

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_MESSAGEtype with an ERROR_MESSAGEtype, 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_MESSAGEmessage type, like this:

JOptionPane.WARNING_MESSAGE

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

JOptionPane showMessageDialog - warning message icon example

A plain message

You can also specify the PLAIN_MESSAGEtype, 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

 

Good work

Good sample work for show JOptionPane message...

Post new comment

The content of this field is kept private and will not be shown publicly.