Show Dialog Box in Java

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

Show Dialog Box in Java - Swing Dialogs


Message dialog box is used to display informative messages to the user. In this section we will use JOptionPane class to display the message Dialog box. Our program display "Click Me" button on the window and when user clicks on it program displays Message box with "OK" button and message "". 

When you run the program following window will be displayed:

When you click on "Click Me" button, following Message is displayed:

Program description:

JOptionPane Class:

In non-swing application we were using class for input or output some text or numeric values but now in the swing application we can use JOptionPane to show the output or show the message. This way of inputting or outputting works very efficiently in the Swing Applications. The window for showing message for input or output makes your application very innovative.

JOptionPane class is available in the javax.swing.*; package. This class provide various types of message dialog box as follows:

  • A simple message dialog box which has only one button i.e. "Ok". This type of message dialog box is used only for showing the appropriate message and user can finish the message dialog box by clicking the "Ok" button.
  • A message dialog box which has two or three buttons. You can set several values for viewing several message dialog box as follows:
    1.)  "Yes" and "No"
    2.)  "Yes", "No" and "Cancel"
    3.)  "Ok", and "Cancel"
  • A input dialog box which contains two buttons "Ok" and "Cancel".

The JOptionPane class has three methods as follows:

  • showMessageDialog(): First is the showMessageDialog() method which is used to display a simple message.
  • showInputDialog(): Second is the showInputDialog() method which is used to display a prompt for inputting. This method returns a String value which is entered by you.
  • showConfirmDialog(): And the last or third method is the showConfirmDialog() which asks the user for confirmation (Yes/No) by displaying message. This method return a numeric value either 0 or 1. If you click on the "Yes" button then the method returns 1 otherwise 0.

How program Works:

This program illustrates you how to show a message dialog box when you click on the button.

This method is used to show a message dialog box which contains some text messages. This is being used with two arguments in the program where the first argument is the parent object in which the dialog box opens and another is the message which has to be shown.

Here is the code of the program:

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

public class ShowDialogBox{
  JFrame frame;
  public static void main(String[] args){
  ShowDialogBox db = new ShowDialogBox();

  public ShowDialogBox(){
  frame = new JFrame("Show Message Dialog");
  JButton button = new JButton("Click Me");
  button.addActionListener(new MyAction());

  public class MyAction implements ActionListener{
  public void actionPerformed(ActionEvent e){

Download this example.

Share on Google+Share on Google+

Show Dialog Box in Java

Posted on: April 14, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Show Dialog Box in Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 21, 2012
java examples

i want more details of new example
March 23, 2012

thanks bro
April 8, 2012

Hey I downloaded your example , I Putted it into EditPlus 3 so . then I'm trying to compile it but it's showing "Output completed" ... - normal termination , or W.E I'm waiting . Waiting . but it's not opening the box or W.E it is : / Thank You! P.S:I know my english is not the best try to understand ;)
April 30, 2012

Thanks Dude !! This code helped me a lot.
jhan lee
September 18, 2012

graet but this is not just what i need...
George M
December 30, 2012

At this point in my learning, I am more impressed by your work than most others could possibly be. This is meant as a compliment, I truly cannot wait until I become useful in the programming world.
kishan lodhari
December 1, 2012

i am very happy because my problem are solve .......:)
May 4, 2013

compileation error
July 1, 2011
Java MessageDialogBox

how to put an image in dialog box????