Of course! In Java, showing a message box to the user is a common task. The standard and most straightforward way to do this is by using the JOptionPane class from the Swing library.

Here’s a complete guide, from the basics to more advanced usage.
The Basics: JOptionPane.showMessageDialog
This is the simplest method. It displays a dialog box with a message, an icon, and a single "OK" button.
Key Components:
- Parent Component: Usually a
JFrame. This helps the dialog box be centered over your application window. If you passnull, the dialog will appear in the center of the screen. - Message: The text you want to display.
- Title: The text for the dialog's title bar.
- Message Type: This determines the icon and default button text. Common types are:
JOptionPane.PLAIN_MESSAGE: No icon.JOptionPane.INFORMATION_MESSAGE: An 'i' in a circle.JOptionPane.WARNING_MESSAGE: A yellow exclamation mark triangle.JOptionPane.ERROR_MESSAGE: A red 'X' in a circle.JOptionPane.QUESTION_MESSAGE: A question mark.
Complete Example (Swing Application)
This is a runnable example. It creates a simple window with a button. When you click the button, a message box appears.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MessageBoxExample {
public static void main(String[] args) {
// Create the main window (JFrame)
JFrame frame = new JFrame("Message Box Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);
frame.setLayout(new FlowLayout());
// Create a button
JButton showButton = new JButton("Show Message Box");
// Add an action listener to the button
showButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Show the message box when the button is clicked
// The 'frame' is the parent component, so the dialog will be centered on it.
JOptionPane.showMessageDialog(frame,
"This is an informational message.",
"Information",
JOptionPane.INFORMATION_MESSAGE);
}
});
// Add the button to the frame
frame.add(showButton);
// Make the frame visible
frame.setVisible(true);
}
}
How to Run:
- Save the code as
MessageBoxExample.java. - Compile it:
javac MessageBoxExample.java - Run it:
java MessageBoxExample
You will see a window. Click the button to see the message box.

Different Types of Message Boxes
Here’s how to create different kinds of dialogs using the same showMessageDialog method, just by changing the messageType.
Error Message
JOptionPane.showMessageDialog(frame,
"An error occurred while processing your request.",
"Error",
JOptionPane.ERROR_MESSAGE);
Icon: Red 'X'
Warning Message
JOptionPane.showMessageDialog(frame,
"This action cannot be undone.",
"Warning",
JOptionPane.WARNING_MESSAGE);
Icon: Yellow exclamation mark
Plain Message (No Icon)
JOptionPane.showMessageDialog(frame,
"Operation completed successfully.",
"Success",
JOptionPane.PLAIN_MESSAGE);
Icon: None

Getting User Input with showConfirmDialog
Sometimes you need to ask the user a "Yes/No/Cancel" question. For this, you use showConfirmDialog.
It returns an int value representing the user's choice:
JOptionPane.YES_OPTIONJOptionPane.NO_OPTIONJOptionPane.CANCEL_OPTIONJOptionPane.CLOSED_OPTION(if the user closes the dialog without clicking a button)
int reply = JOptionPane.showConfirmDialog(null,
"Do you want to continue?",
"Confirm Action",
JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
System.out.println("User chose YES.");
} else {
System.out.println("User chose NO or closed the dialog.");
}
Options for showConfirmDialog:
JOptionPane.YES_NO_OPTIONJOptionPane.YES_NO_CANCEL_OPTIONJOptionPane.OK_CANCEL_OPTION
Getting Text Input with showInputDialog
If you need to ask the user for a text input, showInputDialog is perfect.
It returns the user's input as a String. If the user cancels or closes the dialog, it returns null.
// Simple version
String name = JOptionPane.showInputDialog(null, "What is your name?");
if (name != null && !name.trim().isEmpty()) {
System.out.println("Hello, " + name + "!");
} else {
System.out.println("Name was not entered.");
}
// Version with a title and initial text
String ageInput = JOptionPane.showInputDialog(null,
"Please enter your age:",
"Age Verification",
JOptionPane.QUESTION_MESSAGE);
try {
int age = Integer.parseInt(ageInput);
System.out.println("You are " + age + " years old.");
} catch (NumberFormatException e) {
System.out.println("Invalid age entered.");
}
Creating a Custom Dialog with showOptionDialog
This is the most powerful method. It allows you to create a dialog with a custom message, icon, button options, and initial selection.
// The options for the buttons
Object[] options = {"Yes, please", "No, thanks", "Maybe"};
// Show the option dialog
int n = JOptionPane.showOptionDialog(null,
"Would you like to enable auto-updates?",
"Auto-Update Settings",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, // Use default icon
options, // The button texts
options[2]); // The default button (Maybe)
// Handle the response
switch (n) {
case 0: // Yes
System.out.println("Auto-updates enabled.");
break;
case 1: // No
System.out.println("Auto-updates disabled.");
break;
case 2: // Maybe
System.out.println("Auto-updates will be asked again later.");
break;
default: // Closed the dialog
System.out.println("Dialog was closed.");
break;
}
Summary Table
| Method | Purpose | Return Value |
|---|---|---|
showMessageDialog |
Display an informational message. | void |
showConfirmDialog |
Ask a "Yes/No/Cancel" question. | int (YES, NO, CANCEL, CLOSED) |
showInputDialog |
Ask the user for a text input. | String (user input or null) |
showOptionDialog |
Create a fully custom dialog with your own buttons. | int (index of the selected button) |
Important Prerequisite
All these JOptionPane classes are part of Java Swing. To use them, you must have a graphical environment. If you are running your Java application on a headless server (without a display), these methods will throw an java.awt.HeadlessException. For simple command-line applications, you would typically use System.out.println() for output and a Scanner for input.
