Create and Handle Dialog Message in Java
Java provides GUI (Graphical user interface), with panels, buttons, labels, text fields and a lot more to make the user experience better. You can create a JFrame through drag drop in NetBeans as well as you can code a custom JFrame and can add buttons to it. Java provides a feature to show a complete dialog box to show some dialog message or error or any kind of confirmation from the user. Java provides a complete library to show these dialog boxes, you can just import JOptionsPane into your project and show any kind of dialog box without writing the design code for custom message dialog box separately. JOptionPane helps to display a complete popup dialog box with one line code.
JOptionPane:
Import JOptionPane:
Before using JOptionPane you must import JOptionPane library in your project by using import keyword. JOptionPane is a Java swing component so you need to access it through javax.swing.
1 |
import javax.swing.JOptionPane; |
Components of JOptionPane Dialog Box:
- Component:
You can add any Java component in showConfirmDialog. - Info Message:
Information You want to show to the user. - Title Bar:
Text on the title bar. - Icon:
Icon, you want to show beside the message.
Type of icons:
YES_NO_OPTION
YES_OPTION
YES_NO_CANCEL_OPTION
ERROR_MESSAGE
INFORMATION_MESSAGE
OK_CANCEL_OPTION
OK_OPTION
PLAIN_MESSAGE
QUESTION_MESSAGE
WARNING_MESSAGE
Types Of JOptionPane:
- showConfirmDialog
showConfirmDialog asks for the user Confirmation, whether to do something or not. - showInputDialog
showInputDialog asks for the user input on some action. - showMessageDialog
showMessageDialog shows any kind of message to the user, in case if the user did something wrong, or any info message. - showOptionDialog
showOptionDialog is the combination of showConfirmDialog, showInputDialog and showMessageDialog.
Syntax:
1 |
JOptionPane.OptionPaneType(component, message, title, icon); |

showConfirmDialog:
Syntax:
JOptionPane.showConfirmDialog(component, message, title, icon);
Example:
1 |
JOptionPane.showConfirmDialog(null, "Are you sure?", "Confirmation Message", JOptionPane.YES_NO_OPTION); |

Handle User Action on Confirmation Dialog:
showConfirmDialog return integer value, in case if the user presses yes, it will return JOptionPane.YES_OPTION.
1 |
JOptionPane.showConfirmDialog(null, "Are you sure?", "Confirmation Message", JOptionPane.YES_NO_OPTION); |
1 2 3 |
if(dialogResult == JOptionPane.YES_OPTION){ //Do Something } |
showInputDialog:
Syntax:
JOptionPane.showInputDialog(component, message, title, icon);
Example:
1 |
JOptionPane.showInputDialog(null, "Enter Your Name: ", "User Name", JOptionPane.PLAIN_MESSAGE); |

Handle User Action on Input Dialog:
showInputDialog return String value. You can easily get it through a String.
1 |
String Name = JOptionPane.showInputDialog(null, "Enter Your Name: ", "User Name", JOptionPane.PLAIN_MESSAGE); |
showMessageDialog:
Syntax:
JOptionPane.showMessageDialog(component, message, title, icon);
Example:
1 |
JOptionPane.showMessageDialog(null, "Record Updated", "Record Status", JOptionPane.INFORMATION_MESSAGE); |

showOptionDialog:
Syntax:
JOptionPane.showOptionDialog(component, message, title, icon);
Example:
1 2 3 4 5 6 7 8 9 |
Object[] options = {"Male","Female"}; JOptionPane.showOptionDialog(null, //Component parentComponent "What is your gender?", //Object message, "Choose an option", //String title JOptionPane.YES_NO_OPTION, //int optionType JOptionPane.INFORMATION_MESSAGE, //int messageType null, //Icon icon, options, //Object[] options, "Male");//Object initialValue |

Handle User Option for Option Dialog:
showOptionDialog return integer value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Object[] options = {"Male","Female"}; int gender = JOptionPane.showOptionDialog(null, //Component parentComponent "What is your gender?", //Object message, "Choose an option", //String title JOptionPane.YES_NO_OPTION, //int optionType JOptionPane.INFORMATION_MESSAGE, //int messageType null, //Icon icon, options, //Object[] options, "Male");//Object initialValue if(gender == 0 ){ //Male was chosen }else{ //Female was chosen } |