Introduction
In this article, we will learn about JOptionPane in Java Programming Language with its implementation.
JOptionPane is generally used to access the standard dialog boxes like confirm dialog box, message dialog box, and input dialog box.
These dialog windows are used to present information to the user or to solicit input from them. The JOptionPane class is a descendant of the JComponent class.

Also check out Swing component in java
Class Declaration
You can also try this code with Online Java Compiler |
Commonly used Constructors of JOptionPane Class
Below are mentioned commonly used Constructors of JOptionPane Class:
- JOptionPane(): It's used to make a JOptionPane that displays a test message.
- JOptionPane(Object message): It's used to make a JOptionPane instance for displaying a message.
- JOptionPane(Object message, messageType): It's used to make a JOptionPane instance that displays a message with the message type and default options supplied.
Commonly used Methods of JOptionPane Class
Below are mentioned commonly used Methods of JOptionPane Class:
- JDialog createDialog(String title): It's used to make a new parentless JDialog with the supplied title and then return it.
- static void showMessageDialog(Component parentComponent, Object testmessage): It is used to make a dialog with the title "Message," which is an information message.
- static void showMessageDialog(Component parentComponent, Object testmessage, String title, int messageType): It's used to make a message dialog with the title and messageType that you specify.
- static int showConfirmDialog(Component parentComponent, Object message): It's used to make a dialog box containing the options, Yes, No, and Cancel, as well as the title, Select an Option.
- static String showInputDialog(Component parentComponent, Object message): It's used to display a question-message dialogue that asks the user for input and is parented to parentComponent.
- void setInputValue(Object newValue): It's used to set the input value that the user chose or typed in.
Let us see some examples of JOptionPane:

showMessageDialog()
Program:
You can also try this code with Online Java Compiler |
Output:

showInputDialog()
Program:
You can also try this code with Online Java Compiler |
Output:

showConfirmDialog()
Program:
You can also try this code with Online Java Compiler |
Output:




