showMessageDialog() - Displaying Message Dialog Boxes

This section provides a tutorial example on how to use the static method, showMessageDialog(), to create and display message dialog boxes for 4 different type of messages: information, warning, error, and plain.

The simplest dialog box you can create and display with the javax.swing.JOptionPane class is the message dialog box. This can be done with the static method: showMessageDialog(frame, message, title, type), where:

Here is an example program I wrote to test the showMessageDialog() method:

 * Copyright (c) 2014,, All Rights Reserved.
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JOptionPaneShowMessageDialog implements ActionListener {
   JFrame myFrame = null;
   public static void main(String[] a) {
      (new JOptionPaneShowMessageDialog()).test();
   private void test() {
      myFrame = new JFrame("showMessageDialog Test");
      myFrame.setContentPane(new JDesktopPane());
      JMenuBar myMenuBar = new JMenuBar();
      JMenu myMenu = getDialogMenu();
   private JMenu getDialogMenu() {
      JMenu myMenu = new JMenu("Dialogs");
      JMenuItem myItem = new JMenuItem("Information");
      myItem = new JMenuItem("Warning");
      myItem = new JMenuItem("Error");
      myItem = new JMenuItem("Plain");
      return myMenu;
   public void actionPerformed(ActionEvent e) {
      String menuText = ((JMenuItem) e.getSource()).getText();
      int messageType = JOptionPane.INFORMATION_MESSAGE;
      if (menuText.equals("Information")) {
      	 messageType = JOptionPane.INFORMATION_MESSAGE;
      } else if (menuText.equals("Warning")) {
      	 messageType = JOptionPane.WARNING_MESSAGE;
      } else if (menuText.equals("Error")) {
      	 messageType = JOptionPane.ERROR_MESSAGE;
      } else if (menuText.equals("Plain")) {
      	 messageType = JOptionPane.PLAIN_MESSAGE;

      System.out.println("Before displaying the dialog: "+menuText);
         "This is message dialog box of type: "+menuText,
         menuText+" Message", messageType);
      System.out.println("After displaying the dialog: "+menuText);

If you run this example, open the "Dialogs" menu, and click "Information", "Warning" or "Error" menu item, you will see a message dialog box showing up like this:
Message Dialog Box

Sample programs listed in this section have been tested with JDK 1.6.0 to 1.8.0.

Last update: 2014.

Table of Contents

 About This Book

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 Menu Bar, Menus, Menu Items and Listeners

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

Option Dialog Boxes

 javax.swing.JOptionPane - Creating and Displaying Option Dialog Boxes

showMessageDialog() - Displaying Message Dialog Boxes

 showConfirmDialog() - Displaying Confirmation Dialog Boxes

 Receiving Inputs from Confirmation Dialog Boxes

 showInputDialog() - Displaying Input Dialog Boxes

 showOptionDialog() - Displaying Option Dialog Boxes

 showInternal*Dialog() - Displaying Internal Dialog Boxes

 createDialog() - Creating Dialog Boxes Directly

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker


 PDF Printing Version