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

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.