Open In App

Java JOptionPane

Last Updated : 07 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Constructors of JOptionPane Class

Constructors

Description

JOptionPane()

This is the default constructor for JOptionPane.It is used to create a JOptionPane with no options and message.

JOptionPane(Object message)

It creates a message dialog with a specified message.

JOptionPane(Object message, int messageType)

It creates a message dialog with a specified message and its Type.

JOptionPane(Object message, int messageType, int optionType)

It helps us to create a dialog with a specified message, message type, and option type.

Methods of JOptionPane

Methods

Description

createDialog(String title)

It helps us to create JDialog with a specified title but without any parent.

showMessageDialog(Component parentComponent, Object message)

This method displays a message dialog with the specified message.

showInputDialog(Component parentComponent, Object message)

This method displays an input dialog with the specified message.

setMessageType(int messageType)

This method to set the message type of the dialog

setOptionType(int optionType)

This method allows you to set the option type for the dialog.

setOptions(Object[] options)

Here we can set a list of custom options.

setInitialValue(Object initialValue)

This method sets the initial selection when using custom options in the dialog.

Fields of JOptionPane

Fields

Description

int ERROR_MESSAGE

A Constant code displaying an error message icon.

int INFORMATION_MESSAGE

A Constant code displaying information message icon.

int WARNING_MESSAGE

A Constant code for displaying Warning message icon.

int YES_NO_OPTION

A constant for creating a dialog with “Yes” and “No” options.

int YES_NO_CANCEL_OPTION

A constant for creating a dialog with “Yes,” “No,” and “Cancel” options.

int OK_CANCEL_OPTION

A constant for creating a dialog with “OK” and “Cancel” options.

Classes from Which JOptionPane Methods are inherited

  • java.awt.Component
  • javax.swing.JComponent
  • java.io.Serializable

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:

Message Dialog 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:

Input Dialog Output

Final Output After Selection:

Input Message Dialog

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:

Confirm Dialog Output

Final Output After Selection:

Confirm Message Dialog Output

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:

Option Dialog Output

Final Output After Selection:

Option Message Dialog Output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads