Java JOptionPane
Last Updated :
07 Nov, 2023
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
This is the default constructor for JOptionPane.It is used to create a JOptionPane with no options and message.
|
It creates a message dialog with a specified message.
|
It creates a message dialog with a specified message and its Type.
|
It helps us to create a dialog with a specified message, message type, and option type.
|
Methods of JOptionPane
It helps us to create JDialog with a specified title but without any parent.
|
This method displays a message dialog with the specified message.
|
This method displays an input dialog with the specified message.
|
This method to set the message type of the dialog
|
This method allows you to set the option type for the dialog.
|
Here we can set a list of custom options.
|
This method sets the initial selection when using custom options in the dialog.
|
Fields of JOptionPane
A Constant code displaying an error message icon.
|
A Constant code displaying information message icon.
|
A Constant code for displaying Warning message icon.
|
A constant for creating a dialog with “Yes” and “No” options.
|
A constant for creating a dialog with “Yes,” “No,” and “Cancel” options.
|
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
import javax.swing.JOptionPane;
public class MessageDialogExample {
public static void main(String[] args) {
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
import javax.swing.JOptionPane;
public class InputDialogExample {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog( "Enter your Article Name:" );
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
import javax.swing.JOptionPane;
public class ConfirmDialogExample {
public static void main(String[] args) {
int choice = JOptionPane.showConfirmDialog( null , "Do you want to save changes?" ,
"Confirmation" , JOptionPane.YES_NO_CANCEL_OPTION);
if (choice == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog( null , "Changes saved." );
} else if (choice == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog( null , "Changes not saved." );
} else {
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
import javax.swing.JOptionPane;
public class OptionDialogExample {
public static void main(String[] args)
{
Object[] options = { "Yes" , "No" , "Cancel" };
int choice = JOptionPane.showOptionDialog(
null ,
"Do you want to proceed?" ,
"Custom Options" ,
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null ,
options,
options[ 2 ]
);
if (choice == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog( null , "Proceeding..." );
}
else if (choice == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog( null , "Not proceeding." );
}
else {
JOptionPane.showMessageDialog( null , "Operation canceled." );
}
}
}
|
Output:
Final Output After Selection:
Share your thoughts in the comments
Please Login to comment...