Table of contents
1.
Introduction
2.
Class Declaration
2.1.
Commonly used Constructors of JOptionPane Class
2.2.
Commonly used Methods of JOptionPane Class
2.2.1.
showMessageDialog()
2.2.2.
showInputDialog()
2.2.3.
showConfirmDialog()
3.
Frequently Asked Questions
3.1.
What is the JOptionPane input dialog?
3.2.
What are the types of Dialogbox in Java?
3.3.
What is Java GUI?
3.4.
Is JOptionPane a GUI?
3.5.
What is the JFrame class in Java?
4.
Conclusion
Last Updated: Sep 18, 2025

JOptionPane in Java – Dialog Box Examples & Uses

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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

public class JOptionPane 
extends JComponent 
implements Accessible  
You can also try this code with Online Java Compiler
Run Code

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:

import javax.swing.*;
public class OptionPaneTest {
    JFrame testframe;
    OptionPaneTest() {
        testframe = new JFrame();
        JOptionPane.showMessageDialog(testframe, "Hello Ninjas, Welcome to Coding Ninjas.");
    }
    public static void main(String[] args) {
        new OptionPaneTest();
    }
}
You can also try this code with Online Java Compiler
Run Code

Output:

 

showInputDialog()

Program:

import javax.swing.*;
public class OptionPaneTest {
    JFrame testframe;
    OptionPaneTest() {
        testframe = new JFrame();
        String testname = JOptionPane.showInputDialog(testframe, "Enter Name");
    }
    public static void main(String[] args) {
        new OptionPaneTest();
    }
}
You can also try this code with Online Java Compiler
Run Code

Output:

 

showConfirmDialog()

Program:

import javax.swing.*;
import java.awt.event.*;
public class OptionPaneTest extends WindowAdapter {
    JFrame testframe;
    OptionPaneTest() {
        testframe = new JFrame();
        testframe.addWindowListener(this);
        testframe.setSize(300, 300);
        testframe.setLayout(null);
        testframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        testframe.setVisible(true);
    }
    public void windowClosing(WindowEvent ev) {
        int x = JOptionPane.showConfirmDialog(testframe, "Are you really sure?");
        if (x == JOptionPane.YES_OPTION) {
            testframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    public static void main(String[] args) {
        new OptionPaneTest();
    }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Frequently Asked Questions

What is the JOptionPane input dialog?

A dialog box is a small window (graphical) that is used to request input from the user or display a message to the user. The two of the dialog boxes that are commonly used are: Message Dialog: It is a dialog box that displays a message. 

Input Dialog: It is a dialog box that prompts the user for input. 

The 'javax.swing.JOptionPane' class offers dialog box methods.

What are the types of Dialogbox in Java?

In Java Swing, we can create two kinds of dialogs: standard dialogs and custom dialogs. Custom dialogs are created by programmers. They are based on the JDialog class. Standard dialogs are predefined dialogs available in the Swing toolkit, for example, the JColorChooser or the JFileChooser.

What is Java GUI?

Graphical User Interface (GUI) in Java is an easy-to-use visual experience builder that is used for applications that are developed in Java. It is mainly made of components such as buttons, labels, windows, etc., through which the user can interact with an application. GUI plays an essential role in building accessible interfaces for Java applications.

Is JOptionPane a GUI?

JOptionPane is an excellent way to throw together a few dialog boxes and get a GUI, but it doesn't give us the flexibility we really want. But with power and flexibility comes complexity. The introductory class we start with is a JFrame.

What is the JFrame class in Java?

JFrame class is a type of container inheriting the java. awt. Frame class. Whenever a Graphical Use Interface (GUI) is created with Java Swing functionality, a container is required where components like labels, buttons, and text fields are added to create a Graphical User Interface(GUI) and are known as JFrame.

Conclusion

In this article, we have studied JOptionPane in Java. We have also discussed the different constructors and methods of JOptionPane in Java in detail.

We hope that this article has provided you with the help to enhance your knowledge regarding JOptionPane in Java with different constructors and methods provided in detail and if you would like to learn more, check out our articles on Generics in JavaTrim in Java and Java Regex.

Live masterclass