JOptionPane Dialogs in Java



The JOptionPane is a subclass of the JComponent class, which includes static methods for creating and customizing modal dialog boxes using a simple code. The JOptionPane is used instead of JDialog to minimize the complexity of the code. The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.

Types of Dialogue Boxes in JOptionPane

The JOptionPane class is used to display four types of dialog boxes:

MessageDialog

A dialog box that shows a message and lets you add icons to alert the user.

Syntax

The following is the syntax:

JOptionPane.showMessageDialog(parentComponent, message, title, messageType);

The following are the types of messages we can use in the MessageDialog box:

  • INFORMATION_MESSAGE (??)
  • WARNING_MESSAGE (??)
  • ERROR_MESSAGE (?)

ConfirmDialog

A dialog box that shows a message and lets the user answer a question i.e., user can give input using the available buttons such as Yes/No, Ok/Cancel, etc.

Syntax

The following is the syntax:
int choice = JOptionPane.showConfirmDialog(parentComponent, message, title, optionType, messageType);

The following are the button options in the ConfirmDialog box:

  • YES_NO_OPTION (Yes/No)
  • YES_NO_CANCEL_OPTION (Yes/No/Cancel)
  • OK_CANCEL_OPTION (OK/Cancel)

InputDialog

A dialog box that shows a message and includes an input box for the user to type a response.

Syntax

The following is the syntax:

String input = JOptionPane.showInputDialog(parentComponent, message, title, messageType);

The following are the two variations in the InputDialog box:

  • Text Input: In this, user can simply write the input i.e., text entry.
  • Dropdown Selection: In this, user can give input by choosing an option from the predefined options.

OptionDialog

A dialog box that combines all of the above types, message display, user input, and question answering in one.

Syntax

The following is the syntax:

int choice = JOptionPane.showOptionDialog(
    parentComponent,
    message,
    title,
    optionType,
    messageType,
    icon,
    options,
    initialValue
);

Example of Different Types of JOptionPane Dialogs

Below is an example of different types of JOptionPane dialogs in Java:

import javax.swing.*;
public class JoptionPaneTest {
   public static void main(String[] args) {
      JFrame frame = new JFrame("JoptionPane Test");
      frame.setSize(200, 200);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      JOptionPane.showMessageDialog(frame, "Hello Java");
      JOptionPane.showMessageDialog(frame, "You have less amount, please recharge","Apocalyptic message", JOptionPane.WARNING_MESSAGE);
      int result = JOptionPane.showConfirmDialog(null, "Do you want to remove item now?");
      switch (result) {
         case JOptionPane.YES_OPTION:
         System.out.println("Yes");
         break;
         case JOptionPane.NO_OPTION:
         System.out.println("No");
         break;
         case JOptionPane.CANCEL_OPTION:
         System.out.println("Cancel");
         break;
         case JOptionPane.CLOSED_OPTION:
         System.out.println("Closed");
         break;
      }
      String name = JOptionPane.showInputDialog(null, "Please enter your name.");
      System.out.println(name);
      JTextField userField = new JTextField();
      JPasswordField passField = new JPasswordField();
      String message = "Please enter your user name and password.";
      result = JOptionPane.showOptionDialog(frame, new Object[] {message, userField, passField},
      "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
      if (result == JOptionPane.OK_OPTION)
      System.out.println(userField.getText() + " " + new String(passField.getPassword()));
      System.exit(0);
   }
}

Output

The MessageDialog box with an information message:

The MessageDialog box with a warning message:

The ConfirmDialog box output:

The InputDialog box Output:

The OptionDialog box output:

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-04-11T12:24:55+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements