How do I display a message dialog in Java Swing?

Use JOptionPane.showMessageDialog() to display a short message with an OK button. This example opens an information dialog without creating a separate JFrame.

Source code

Save the following code as MessageDialogExample.java:

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class MessageDialogExample {
    void main() {
        SwingUtilities.invokeLater(this::showMessage);
    }

    private void showMessage() {
        JOptionPane.showMessageDialog(
                null,
                "Hello from Swing!",
                "Message",
                JOptionPane.INFORMATION_MESSAGE);
    }
}

What the dialog looks like

Illustration of a dialog titled Message, with the text Hello from Swing! and an OK button

The icon and window styling depend on your system and Swing look and feel.

Explanation

The showMessageDialog() call uses four arguments:

Argument Purpose
null Shows the dialog without positioning it relative to an existing component
"Hello from Swing!" Sets the message shown inside the dialog
"Message" Sets the title shown in the title bar
JOptionPane.INFORMATION_MESSAGE Selects the information message type, which usually displays an information icon

If your application already has a window, pass its JFrame instead of null so Swing can position the dialog relative to that window.

SwingUtilities.invokeLater() schedules showMessage() on Swing’s event dispatch thread. The dialog is modal, so the call to showMessageDialog() returns after the user dismisses the dialog. It returns no value. If you need to ask a question and read the user’s choice, use showConfirmDialog().

Run the example

javac MessageDialogExample.java
java MessageDialogExample

A dialog titled Message displays Hello from Swing!. Click OK to dismiss it.

How do I create a message dialog box?

This example demonstrate how to create a message dialog box using the JOptionPane class methods. In the code below you’ll see the use of JOptionPane.showMessageDialog(), JOptionPane.showInputDialog() and JOptionPane.showConfirmDialog().

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;

public class MessageDialogDemo extends JFrame {
    public MessageDialogDemo() throws HeadlessException {
        initialize();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new MessageDialogDemo().setVisible(true));
    }

    private void initialize() {
        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JButton button1 = new JButton("Click Me!");
        button1.addActionListener(e -> {
            // Show a message dialog with a text message
            JOptionPane.showMessageDialog((Component) e.getSource(),
                    "Thank you!");
        });

        JButton button2 = new JButton("What is your name?");
        button2.addActionListener(e -> {
            // Show an input dialog that will ask you to input some texts
            String text = JOptionPane.showInputDialog((Component) e.getSource(),
                    "What is your name?");
            if (text != null && !text.equals("")) {
                JOptionPane.showMessageDialog((Component) e.getSource(),
                        "Hello " + text);
            }
        });

        JButton button3 = new JButton("Close Application");
        button3.addActionListener(e -> {
            // Show a confirmation dialog which will ask to for a YES or NO
            // button.
            int result = JOptionPane.showConfirmDialog((Component) e.getSource(),
                    "Are you sure want to close this application?");
            if (result == JOptionPane.YES_OPTION) {
                System.exit(0);
            } else if (result == JOptionPane.NO_OPTION) {
                // Do nothing, continue to run the application
            }
        });

        setLayout(new FlowLayout(FlowLayout.CENTER));
        getContentPane().add(button1);
        getContentPane().add(button2);
        getContentPane().add(button3);
    }
}
Message Dialog Box with JOptionPane
Message Dialog Box with JOptionPane