How to Use JOptionPane to Create Dialogs

JOptionPane enables you to create and customize various kinds of dialogs. With JOptionPane the dialogs can have customized dialog layout, icons, dialog title, dialog text and button text. In addition, JOptionPane allows you to customize the displaying component and its position where on the screen dialog display.

To create simple modal dialogs, you use static methods of JOptionPane showAbcDialog() for instanceshowMessageDialog(). If a dialog has to be in an internal frame you can use showInteralAbcDialog() for example showInternalMessageDialog(). In order to create modeless dialog you need to create new instance of JOptionPane and add it to a JDialog instance. And then you call method setVisible(true)  of the JDialog’s instance to make it visible on screen.

JOptionPane provides you with two useful static methods such as showMessageDialog() and showOptionDialog(). The showMessageDialog() method shows a very simple dialog with one button while showOptionDialog() method displays a highly customized dialogs with different buttons texts. Besides these methods, JOptionPane provides showConfirmDialog() method to ask users to confirm an action and showInputDialog() to get simple input from the users.

Example of Using Method showMessageDialog of JOptionPane

In this example, we use showMessageDialog() method of JOptionPane to show various simple dialog with one button and different icons which represent different message types such as information, warning, error and question message. When you click on each radio button, it will display corresponding message type.

JOptionPane
Information Message
Warning Message
Error Message
Question Message
package joptionpanedemo1;

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

public class Main {

    public static void main(String[] args) {
        final JFrame frame = new JFrame("JOptionPane Demo");

        // implement ItemListener interface
        class MyItemListener implements ItemListener {

            public void itemStateChanged(ItemEvent ev) {
                boolean selected = (ev.getStateChange() == ItemEvent.SELECTED);
                AbstractButton button = (AbstractButton) ev.getItemSelectable();
                String command = button.getActionCommand();
                if (selected) {
                    int messageType = -1;
                    String message = "";
                    if (command.equals("INFORMATION")) {
                        messageType = JOptionPane.INFORMATION_MESSAGE;
                        message = "Information Message";
                    } else if (command.equals("WARNING")) {
                        messageType = JOptionPane.WARNING_MESSAGE;
                        message = "Warning Message";
                    } else if (command.equals("ERROR")) {
                        messageType = JOptionPane.ERROR_MESSAGE;
                        message = "Error Message";
                    } else if (command.equals("QUESTION")) {
                        messageType = JOptionPane.QUESTION_MESSAGE;
                        message = "Question Message";
                    }
                    // show message
                    JOptionPane.showMessageDialog(frame,
                            message,
                            "Message Dialog",
                            messageType);
                }
            }
        }

        JRadioButton r1 = new JRadioButton("Information Message");
        r1.setActionCommand("INFORMATION");

        JRadioButton r2 = new JRadioButton("Warning Message");
        r2.setActionCommand("WARNING");

        JRadioButton r3 = new JRadioButton("Error Message");
        r3.setActionCommand("ERROR");

        JRadioButton r4 = new JRadioButton("Question Message");
        r4.setActionCommand("QUESTION");

        // add event handler
        MyItemListener myItemListener = new MyItemListener();
        r1.addItemListener(myItemListener);
        r2.addItemListener(myItemListener);
        r3.addItemListener(myItemListener);
        r4.addItemListener(myItemListener);

        // add radio buttons to a ButtonGroup
        final ButtonGroup group = new ButtonGroup();
        group.add(r1);
        group.add(r2);
        group.add(r3);
        group.add(r4);

        // Frame setting
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        Container cont = frame.getContentPane();

        cont.setLayout(new GridLayout(0, 1));
        cont.add(new JLabel("Please choose the message type:"));
        cont.add(r1);
        cont.add(r2);
        cont.add(r3);
        cont.add(r4);

        frame.setVisible(true);
    }
}Code language: JavaScript (javascript)

Example of Using Method showOptionDialog to Create Dialogs

In this example, we use showOptionDialog method to show question dialog with as yes/no and yes/no/cancel options.

JOptionPane
JOptionPane showOptionDialog
JOptionPane showOptionDialog
package joptionpanedemo2;

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

public class Main {

    public static void main(String[] args) {
        final JFrame frame = new JFrame("JOptionPane Demo");

        // implement ItemListener interface
        class MyItemListener implements ItemListener {
            public void itemStateChanged(ItemEvent ev) {
                boolean selected = (ev.getStateChange() == ItemEvent.SELECTED);
                AbstractButton button = (AbstractButton) ev.getItemSelectable();
                String command = button.getActionCommand();
                if (selected) {
                    int optionType = -1;

                    if (command.equals("YES_NO_OPTION")) {
                        optionType = JOptionPane.YES_NO_OPTION;
                    } else if (command.equals("YES_NO_CANCEL_OPTION")) {
                        optionType = JOptionPane.YES_NO_CANCEL_OPTION;
                    }

                    JOptionPane.showOptionDialog(frame,
                            "Are you sure to confirm the action you've made?",
                            "JOptionPane Demo",
                            optionType,
                            JOptionPane.INFORMATION_MESSAGE, // icon
                            null,
                            null,
                            null);
                }
            }
        }

        JRadioButton r1 = new JRadioButton("Yes / No Options");
        r1.setActionCommand("YES_NO_OPTION");

        JRadioButton r2 = new JRadioButton("Yes / No / Cancel Options");
        r2.setActionCommand("YES_NO_CANCEL_OPTION");

        // add event handlers
        MyItemListener myItemListener = new MyItemListener();
        r1.addItemListener(myItemListener);
        r2.addItemListener(myItemListener);

        // add radio buttons to a ButtonGroup
        final ButtonGroup group = new ButtonGroup();
        group.add(r1);
        group.add(r2);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        Container cont = frame.getContentPane();
        cont.setLayout(new GridLayout(0, 1));
        cont.add(new JLabel("Please choose the options type:"));
        cont.add(r1);
        cont.add(r2);

        frame.setVisible(true);
    }
}Code language: JavaScript (javascript)