Dialog Message in Java

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.

Components of JOptionPane Dialog Box:

  1. Component:
    You can add any Java component in showConfirmDialog.
  2. Info Message:
    Information You want to show to the user.
  3. Title Bar:
    Text on the title bar.
  4. 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:

JOptionPane input dialog
JOptionPane input dialog

showConfirmDialog:

Syntax:
JOptionPane.showConfirmDialog(component, messagetitle, icon);

Example:

JOptionPane showConfirmDialog
JOptionPane showConfirmDialog

Handle User Action on Confirmation Dialog:

showConfirmDialog return integer value, in case if the user presses yes, it will return JOptionPane.YES_OPTION.

 

showInputDialog:

Syntax:
JOptionPane.showInputDialog(component, messagetitle, icon);

Example:

JOptionPane showInputDialog
JOptionPane showInputDialog

Handle User Action on Input Dialog:

showInputDialog return String value. You can easily get it through a String.

 

showMessageDialog:

Syntax:
JOptionPane.showMessageDialog(component, messagetitle, icon);

Example:

JOptionPane showMessageDialog
JOptionPane showMessageDialog

 

 

showOptionDialog:

Syntax:
JOptionPane.showOptionDialog(component, messagetitle, icon);

Example:

JOptionPane showOptionDialog
JOptionPane showOptionDialog

Handle User Option for Option Dialog:

showOptionDialog return integer value.