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
- 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. - 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)
- String – The third parameter is a String placed as the title of the confirmDialog window.
- int – The int that follows the String is the
OptionType
. The differentOptionTypes
forJOptionPane
, are:- DEFAULT_OPTION
- YES_NO_OPTION
- YES_NO_CANCEL_OPTION
- OK_CANCEL_OPTION
- int – The next int is the
MessageType
. The differentMessageTypes
forJOptionPane
, are:- ERROR_MESSAGE
- INFORMATION_MESSAGE
- WARNING_MESSAGE
- QUESTION_MESSAGE
- PLAIN_MESSAGE
- Icon – The last parameter is an
Icon
that is displayed inside the dialog and overrides the defaultMessageType
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”:
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:
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.
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:
Another simple example using the YES_NO_CANCEL_OPTION:
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:
3. Component, Object, String, int & int
Give your confirmation dialog with error icon:
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:
4. Component, Object, String, int, int & icon
Make your confirmation dialog “prettier”Example with Icon
retrieved from a directory:
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:
Example with Component
set to a frame
:
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:
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
.
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:
Very good Work! Well Done!
This would be more useful if you listed the possible return values and what they mean
thank you the explanation is very clear
You have saved my class project. For some reason I could not figure this out. Thank you!
How do you code get an image
SO USEFUL
It helped me a lot thanks dude.
Thank you! This post was useful for me!