How to show DialogBox in java? Complete JOptionPane tutorial.

We need to show the Dialog Boxes lot of times for showing different messages. Java Swing provides JOptionPane utility.

Lets see how we can use JOptionPane class for showing different types of messages :

JOptionPane :

JOptionPane class belongs to the javax.swing.*; package. The class comes with lot of options which user can use for different purpose. It provides dialog boxes from just simple message dialog box to input dialogs.

A simple message dialog box :

It has only one button "OK" , this message dialog box can be used for showing appropriate message for user. JOptionPane provides support providing icons, specifying the dialog title and text, and even customizing the button text. We can also customize how the components inside the dialog layout and displays and also where the dialog should appear on screen.

JOptionPane provides very interesting feature of specifying the icons. We can use customized icons or we can use default used by JOptionPane which are question, information, warning, and error. Lets see how icons get showed up in Windows and Java Looks and feel.

showMessageDialog() :

We can use this method to create simple modal message dialogs. We can specify the message, icon, and title that the dialog displays. Lets see it example below :

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

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

  public DialogBoxTutorial(){
    frame = new JFrame("Simple Dialog Box Demo");
    JButton button = new JButton("Show Dialog");
    button.addActionListener(new DialogAction());
    frame.setSize(500, 400);

  public class DialogAction implements ActionListener{
    public void actionPerformed(ActionEvent e){
            "This is warning dialog demo.",

Here we are used the warning message option so output of this will be


JOptionPane has showOptionsDialog method which allows to specify options such as buttons, icons, message, title, and so on. The benefit of this method is we can change the text appears on buttons of JOptionPane.

Object[] options = {"Yes",
                    "Yes to All", "No to all"};
        int n = JOptionPane.showOptionDialog(frame,
            "Would you like to replace the demo file? ",
            "Options ",


This function used when we don't need to change the button text.

        int n = JOptionPane.showConfirmDialog(
            "Would you like to replace the file?",
            "Confirm Dialog Options",
            JOptionPane.YES_NO_OPTION //Button appearing on JOptionpane

showInputDialog() Example :

Unlike other dialogs this method doesn't return the int , instead it returns the Object which is normally a String. This method is used to get input from the JOptionPane.

String s = (String)JOptionPane.showInputDialog(
                    "String input text",
                    "Input Dialog Box",
                    "Type text here");

        System.out.println("String "+s);

Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment


Post a Comment