Following are the programs to implement JOptionPane
1. Java program to create a showMessageDialog in JOptionPane
This dialog is used to display messages to the user.
Java
//Java program to create a showMessageDialog in JOptionPane import javax.swing.JOptionPane; public class MessageDialogExample { public static void main(String[] args) { // Create a JOptionPane to display a message dialog // The first parameter (null) specifies the dialog's parent component (usually the main frame) // The second parameter is the message to display ("GFG" in this case) // The third parameter is the title of the dialog ("Geeks Premier League 2023") // The fourth parameter (JOptionPane.INFORMATION_MESSAGE) specifies the message type (information icon) JOptionPane.showMessageDialog( null , "GFG" , "Geeks Premier League 2023" , JOptionPane.INFORMATION_MESSAGE); } } |
Output:
2. Java Program to create a showInputDialog in JOptionPane
This dialog is used to take input from the user.
Java
//Java Program to create a showInputDialog in JOptionPane import javax.swing.JOptionPane; public class InputDialogExample { public static void main(String[] args) { // Prompt the user to enter their article name and store it in the 'name' variable String name = JOptionPane.showInputDialog( "Enter your Article Name:" ); // Create a JOptionPane to display a message with a personalized greeting JOptionPane.showMessageDialog( null , "GFG " + name + "!" ); } } |
Output:
Final Output After Selection:
3. Java Program to create showConfirmDialog in JOptionPane
This dialog displays a confirmation message and allows the user to make a decision.
Java
//Java Program to create showConfirmDialog in JOptionPane import javax.swing.JOptionPane; public class ConfirmDialogExample { public static void main(String[] args) { // Display a confirmation dialog with Yes, No, and Cancel options int choice = JOptionPane.showConfirmDialog( null , "Do you want to save changes?" , "Confirmation" , JOptionPane.YES_NO_CANCEL_OPTION); // Check the user's choice and display a corresponding message if (choice == JOptionPane.YES_OPTION) { // If the user chose 'Yes', show a message indicating that changes are saved JOptionPane.showMessageDialog( null , "Changes saved." ); } else if (choice == JOptionPane.NO_OPTION) { // If the user chose 'No', show a message indicating that changes are not saved JOptionPane.showMessageDialog( null , "Changes not saved." ); } else { // If the user chose 'Cancel' or closed the dialog, show a message indicating the operation is canceled JOptionPane.showMessageDialog( null , "Operation canceled." ); } } } |
Output:
Final Output After Selection:
4. Java Program to create a showOptionDialog in JOptionPane
This dialog allows you to create a customized option dialog.
Java
// Java Program to create a // showOptionDialog in JOptionPane import javax.swing.JOptionPane; // Driver Class public class OptionDialogExample { // main function public static void main(String[] args) { // Define an array of custom options for the dialog Object[] options = { "Yes" , "No" , "Cancel" }; // Display an option dialog with custom options // The user's choice is stored in the 'choice' // variable int choice = JOptionPane.showOptionDialog( null , // Parent component (null means center on screen) "Do you want to proceed?" , // Message to display "Custom Options" , // Dialog title JOptionPane.YES_NO_CANCEL_OPTION, // Option type (Yes, No, Cancel) JOptionPane.QUESTION_MESSAGE, // Message type (question icon) null , // Custom icon (null means no custom icon) options, // Custom options array options[ 2 ] // Initial selection (default is "Cancel") ); // Check the user's choice and display a // corresponding message if (choice == JOptionPane.YES_OPTION) { // If the user chose 'Yes' // show a message indicating that they are // proceeding JOptionPane.showMessageDialog( null , "Proceeding..." ); } else if (choice == JOptionPane.NO_OPTION) { // If the user chose 'No' // show a message indicating that they are not // proceeding JOptionPane.showMessageDialog( null , "Not proceeding." ); } else { // If the user chose 'Cancel' or closed the // dialog // show a message indicating the operation is // canceled JOptionPane.showMessageDialog( null , "Operation canceled." ); } } } |
Output:
Final Output After Selection:
Java JOptionPane
In Java, JOptionPane is a part of the Java Swing library. It helps us to create dialog boxes such as message dialogs, conformation dialogs, input dialogs, and options dialogs In this article, we are going to explore some constructors, methods, and some examples of JOptionPane.