Main Tutorials

Java Swing – JOptionPane showConfirmDialog example

This is a review of the showConfirmDialog() method of JOptionPane Class. This method is a quick and easy way to get user input by asking a confirming question, like yes/no/cancel . The showConfirmDialog() can be called using the following combinations of parameters:


Component, Object
Component, Object, String, int
Component, Object, String, int, int
Component, Object, String, int, int, Icon
  1. Component – The first parameter is a Component which determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used.
  2. Object – The second parameter can be any Object. (In some older versions of Java you might get a compiler error when using primitive types directly)
  3. String – The third parameter is a String placed as the title of the confirmDialog window.
  4. int – The int that follows the String is the OptionType. The different OptionTypes for JOptionPane, are:
    • DEFAULT_OPTION
    • YES_NO_OPTION
    • YES_NO_CANCEL_OPTION
    • OK_CANCEL_OPTION
  5. int – The next int is the MessageType. The different MessageTypes for JOptionPane, are:
    • ERROR_MESSAGE
    • INFORMATION_MESSAGE
    • WARNING_MESSAGE
    • QUESTION_MESSAGE
    • PLAIN_MESSAGE
  6. Icon – The last parameter is an Icon that is displayed inside the dialog and overrides the default MessageType icon.

1. Component & Object

Simplest way to get user input. The showConfirmDialog() will bring up a dialog with the options Yes, No and Cancel and the title “Select an Option”:

ConfirmDialog1.java

package com.mkyong.confirmDialog;

import javax.swing.JOptionPane;

public class ConfirmDialog1 {

    public static void main(String[] args) {

        int input = JOptionPane.showConfirmDialog(null, "Do you like bacon?");
        // 0=yes, 1=no, 2=cancel
        System.out.println(input);

    }
}

Output:

swing-comfirm-dialog-2a

2. Component, Object, String & int

Adding some more information to the confirmation dialog. In this example we get to choose the title of the dialog as well as the optionType. The DEFAULT_OPTION has only an “OK” button. This form of the confirmation dialog is equivalent to a simple showMessageDialog() while giving us the ability to get the user input.

ConfirmDialog2a.java

package com.mkyong.confirmDialog;

import javax.swing.JOptionPane;

public class ConfirmDialog2a {

    public static void main(String[] args) {

        int input = JOptionPane.showConfirmDialog(null, 
                "Click ok if you are ok", "Be ok!", JOptionPane.DEFAULT_OPTION);
        // 0=ok
        System.out.println(input);

    }
}

Output:

swing-comfirm-dialog-2b

Another simple example using the YES_NO_CANCEL_OPTION:

ConfirmDialog2b.java

package com.mkyong.confirmDialog;

import javax.swing.JOptionPane;

public class ConfirmDialog2b {

    public static void main(String[] args) {

        int input = JOptionPane.showConfirmDialog(null, 
                "Do you want to proceed?", "Select an Option...",JOptionPane.YES_NO_CANCEL_OPTION);

	// 0=yes, 1=no, 2=cancel
	System.out.println(input);

    }
}

Output:

swing-comfirm-dialog-2c

3. Component, Object, String, int & int

Give your confirmation dialog with error icon:

ConfirmDialog3.java

package com.mkyong.confirmDialog;

import javax.swing.JOptionPane;

public class ConfirmDialog3 {

    public static void main(String[] args) {

        int input = JOptionPane.showConfirmDialog(null, "Do you want to proceed?", "Select an Option...",
				JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE);
		
	// 0=yes, 1=no, 2=cancel
	System.out.println(input);

    }
}

Output:

swing-comfirm-dialog-2d

4. Component, Object, String, int, int & icon

Make your confirmation dialog “prettier”Example with Icon retrieved from a directory:

ConfirmDialog4a.java

package com.mkyong.confirmDialog;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class ConfirmDialog4a {

    public static void main(String[] args) {

        ImageIcon icon = new ImageIcon("src/images/turtle64.png");
	int input = JOptionPane.showConfirmDialog(null, "Do you like turtles?", "Be honest...",
			JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, icon);

        // 0=yes, 1=no, 2=cancel
	System.out.println(input);

    }
}

Output:

swing-comfirm-dialog-2e

Example with Component set to a frame:

ConfirmDialogInFrame.java

package com.mkyong.confirmDialog;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.Color;

public class ConfirmDialogInFrame extends JFrame{

    public ConfirmDialogInFrame() {
        getContentPane().setBackground(Color.DARK_GRAY);
        setTitle("Confirm Dialog in Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
        setSize(400, 300);
        getContentPane().setLayout(null);
    }

    public static void main(String[] args){
        ImageIcon icon = new ImageIcon("src/images/turtle64.png");
        int input = JOptionPane.showConfirmDialog(new ConfirmDialogInFrame(), 
                "I appear as part of the frame!!", "Customized Dialog", 
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, icon);

        // 0=ok, 2=cancel
        System.out.println(input);
    }
}

Output:

swing-comfirm-dialog-2f

5. A more advanced example

For this example we are passing a JPanel as the Object parameter. The JPanel is customized and has a JLabel added to it. We are also manipulating the size of the OptionPane using a call to UIManager.

ConfirmDialogPanel.java

package com.mkyong.messageDialog;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

public class ConfirmDialogPanel {

	public static void main(String[] args) {

		ImageIcon icon = new ImageIcon("src/images/lock64.png");

		JPanel panel = new JPanel();
		panel.setBackground(new Color(102, 205, 170));
		panel.setSize(new Dimension(200, 64));
		panel.setLayout(null);

		JLabel label1 = new JLabel("This file requires administrator rights.");
		label1.setVerticalAlignment(SwingConstants.BOTTOM);
		label1.setBounds(0, 0, 200, 32);
		label1.setFont(new Font("Arial", Font.BOLD, 10));
		label1.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(label1);

		JLabel label2 = new JLabel("Are you sure you want to continue?");
		label2.setVerticalAlignment(SwingConstants.TOP);
		label2.setHorizontalAlignment(SwingConstants.CENTER);
		label2.setFont(new Font("Arial", Font.BOLD, 10));
		label2.setBounds(0, 32, 200, 32);
		panel.add(label2);

		UIManager.put("OptionPane.minimumSize", new Dimension(300, 120));
		int input = JOptionPane.showConfirmDialog(null, panel, "Admin Rights Confirmation",
				JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, icon);

		// 0=yes, 1=no, 2=cancel
		System.out.println(input);

	}

}

Output:

swing-comfirm-dialog-2g

References

  1. How to Make Dialogs
  2. Class JOption Pane – Java 8 API
  3. Constant Field Values

About Author

author image
Marilena Panagiotidou is a senior at University of the Aegean, in the department of Information and Communication Systems Engineering. She is passionate about programming in a wide range of languages. You can contact her at [email protected] or through her LinkedIn.

Comments

Subscribe
Notify of
8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Stamatakis Stelios
5 years ago

Very good Work! Well Done!

Dave E
9 months ago

This would be more useful if you listed the possible return values and what they mean

random dev
2 years ago

thank you the explanation is very clear

Dalton Davis
2 years ago

You have saved my class project. For some reason I could not figure this out. Thank you!

Zone
2 years ago

How do you code get an image

Elif Yağmur Bölük
3 years ago

SO USEFUL

Dev Parzival
3 years ago

It helped me a lot thanks dude.

Duminic? Aurelian Petre
4 years ago

Thank you! This post was useful for me!