Updated March 17, 2023
Introduction to JOptionPane in Java
The JOptionPane is a class that is used to provide standard dialog boxes. It is a part of Java Swing which is used for creating window-based applications. JOptionPane is a component from Java Swing and it deals with dialog boxes especially. The dialog boxes can be of any type such as confirm dialog box, message dialog box or input dialog box. These dialog boxes can be used to display information to the user or to get input from the user.
Syntax:
public class JOptionPane extends JComponent implements Accessible
JComponent is a base class for Swing Components and Accessible is the main interface for the accessibility package. They both provide a standard mechanism that is required to support swing architecture.
JOptionPane Constructors
Below are the constructors as follows:
- JOptionPane(): It creates an instance of JOptionPane with a default text message.
- JOptionPane(Object message, int messageType): It creates an object of JOptionPane which will display a message with specified message type and default options delivered by User Interface.
- JOptionPane(Object message): It creates an object of JOptionPane which will display a message with plain-message message type and default options delivered by User Interface.
- JOptionPane(Object message, int messageType, int option type): It creates an instance of JOptionPane which will display a message with specified message type and options as well.
- JOptionPane(Object message, int messageType, int optionType, Icon icon): Display a message with specified message type, option type and icon as well.
Methods of JOptionPane in Java
While there is a large number of methods in the JOptionPane class, they all fit in the format of showXxxDialog as below:
- showInputDialog: Prompt user for some input.
- showMessageDialog: Shows the dialog box with a message to the user.
- show confirm dialog: Asks user a confirming question, like yes/no/cancel.
- showOptionDialog: The combination of the above three.
All of these methods also come in showInternalXXX flavor, which will use an internal frame to hold the dialog box. Below are the commonly used methods in JOptionPane class:
Sr.No. |
Methods & Description |
1. | JDialog createDialog(String title): This method is used to create a new instance of parentless JDialog with the provided title in the argument. JDialog is the main class for creating a dialog window. |
2. | JDialog createDialog(Component parentComponent, String title): This method is used to create a new instance of JDialog with the provided title in argument as well this method has the provision of providing parent component for dialog box which we are creating. JDialog is centered on the provided parent component in the frame of parentComponent. |
3. | static void showMessageDialog(Component parentComponent, Object message): This method pops up an information-message dialog box with the title as “Message”. By default, the title is “Message”. It has also parent component which we can pass. The dialog box will be centered on this parent component. See below example 1. |
4. | static void showMessageDialog(Component parentComponent, Object message, String title, int messageType): This method will show information-message with additional customizable parameters. It allows us to specify the title of the dialog box and choose the type of message shown. The option type of message will automatically choose the displaying icon on the dialog box. See below example 2. |
5. | static String shows InputDialog(Component parent component, Object message): This method will pop up a question-message dialog box with the message specified. Here also, we have provision to pass parent component. This method will return then the input provided by the user. We can use the same input further for business logic. See below example 3. |
6. | static int showConfirmDialog(Component parentComponent, Object message): This method will pop up a confirmation message dialog box with three options as Yes, No and Cancel. Each option representing an integer value as 0, 1 and 2 respectively. We have two arguments over here, one is parent component and second is a message to be displayed. The method will then return the appropriate integer value according to the option chosen by the user. See below example 4. |
7 |
showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue): This method is like a combination of above all methods where we can create our customized dialog box as per user requirement. Here developers have the flexibility to pass arguments such as parent component, message, title of dialog box, option type, message type, icon. The initial value represents the default selection of options on the dialog box. |
Almost all the methods from the JOptionPane class are static methods. Mostly we will access them directly without creating an instance of JOptionPane.
Examples of JOptionpane in Java
Below are the examples follow:
1. showMessageDialog()
Code:
simple dialog box with only message:
import javax.swing.JOptionPane;
public class Main extends JOptionPane
{
public static void main (String[] args)
{
JOptionPane.showMessageDialog( null, "Hello World..!" );
}
}
Here, the parent component is passed as null which means that there will be no parent for the dialog box and String argument is text message which will be shown on dialog box as information to the user.
Output:
2. showMessageDialog()
Same as example 1 but with additional title and message type as the argument:
Code:
import javax.swing.JOptionPane;
public class JOptionPaneExample
{
public static void main( String[] args )
{
JOptionPane.showMessageDialog( null, "Something Went Wrong.." , "Error as Title",
JOptionPane.ERROR_MESSAGE );
}
}
Output:
3. showInputDialog()
Take input from the user and display it on a dialog box.
Code:
import javax.swing.JOptionPane;
public class JOptionPaneExample extends JOptionPane
{
public static void main( String[] args )
{
String name = JOptionPane.showInputDialog( "Provide User Name:" );
JOptionPane.showMessageDialog(null, name);
}
}
Output:
4. showConfirmDialog()
Code:
package swing.demo.dialogBox;
import javax.swing.JOptionPane;
public class JOptionPaneExample
{
public static void main( String[] args )
{
int input = JOptionPane.showConfirmDialog(null, "Are you Sure?");
// 0=yes, 1=no, 2=cancel
System.out.println(input);
}
}
Output:
Conclusion
In windows-based applications, Java Swing makes it very easy to develop them and it is a very powerful API. JOptionPane being a part of it simplifies creating dialog boxes in an easy manner. It provides standard dialog boxes such as the input dialog box, confirms dialog box and message dialog box. We can create our own custom dialog boxes as well. Since JOptionPane provides standard dialog boxes, it is very useful.
Recommended Articles
This is a guide to JOptionPane in Java. Here we discuss the constructor, methods, and examples of JOptionPane in java. You can also go through our other related articles to learn more –