Java Message Box


Java Message Box

In this section we will use JOptionPane class to display the message Dialog box.

In this section we will use JOptionPane class to display the message Dialog box.

Java Message Box

In this tutorials you will learn about how to create Message Box in Java. Java API provide a class to create a message Box in java. By message box we mean a box in which message is displayed or a box from which input provided by the user can be retrieved. A message box is meant to carry a temporary information or message apart from the main swing application. Most of the message box is used for error message. Using JOptionPane class you can create a message box. Here is the code that creates Message box and shows it:

JOptionPane.showMessageDialog(frame, "This is a Message Box.");

This statement will display a message with message as "This is a Message Box.".

There are  several feature JOptionPane like customizing icon, customizing the component the dialog display and specify where message box should appear on screen.

JOptionPane icon let you decide which icon you want to display. You can use custom icon, or no icon, or any of the four JOptionPane standard icon. The two most useful method of showxxxdialog method are as follows :

  •  showMessageDiaolog : The showMessageDialog method will display the one button dialog method.
  •  showOptionDialog :  This method display a variety of button with button text. and contain a standard text message.
  • showConfirmDialog : It will ask for confirmation of something with Yes/No.
  • showInputDialog : This will display a message box that accept input from user from text field or combo box.

showMessageDialog : Display a message with one button which is labeled "OK". You can specify the message, title, and icon according to your wish. Here are some example using showMessageDialog :

JOptionPane.showMessageDialog(frame," Message Box Demo.");

JOptionPane.showMessageDialog(frame,"Message Box Demo.","Warning",JOptionPane.WARNING_MESSAGE);


JOptionPane.showMessageDialog(frame,"Message Box Demo","Error Message Box",JOptionPane.ERROR_MESSAGE);

showOptionDialog : Displays a Message with the specified buttons, icons, message, title, and so on. By the help of this message, you can change the text that appears on the buttons of the message Box.

Object[] options = {"Yes","No","ask later"};
int n = JOptionPane.showOptionDialog(frame,
"Would you like to save it",null, JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[2]);

JOptionPane () :  Creates a JOptionPane with the specified buttons, icons, message, title, and so on.

Example : We create a program which will display the message box using a swing. First creating a swing application in which we created a button. When you click on the button then message box will pop up and display a message.

import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.event.*;

public class MessageBox {
	JFrame frame;

	public static void main(String[] args) {
		MessageBox db = new MessageBox();

	public MessageBox() {
		frame = new JFrame("Show Message Box");
		JButton button = new JButton("Click");
		button.setAlignmentX((float) 100.00);

		button.addActionListener(new MyAction());
		frame.setSize(300, 300);

	public class MyAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(frame, "Message Box");

Output from the program :

When you click on button then a message box will appear as below:

Download Source Code