JOptionPane in java:
JOptionPane in java is a class which is used to create different types of input and output dialog box with error messages, information message, warning message, question message icon, and plain message. JOptionPane is a class. It is available in javax.swing package We can create an input and output dialog boxes using the following format:
JOptionPane.method_name(parameter_list)
Here:
method_name is the name of the JOptionPane method.
The various methods of JOptionPane in Java are as follow:
-static String showInputDialog(Object message)
-static String showInputDialog(Components parent, object message)
-static String showInputDialog(Component parent, object message ,object default)
-static String showInputDialog(Component parent, object ,String title, int message Type)
-static String showInputDialog(Component parent, object default)
Here the parameter default - default value to present to the user.
The showInputDialog() method is used to display a dialogue box with a message prompt, an input field and two buttons that are “ok” and “cancel”.This method returns the string which user types.
-static void showMessageDialog(Component parent, object message)
-static void showMessageDialog(Component parent, object, string title, int message type)
-static void showMessageDialog(Component parent, object message, String title, int message type, Icon icon)
The showMessageDialog () method is used to display a dialog box with a message prompt and an ok button.
Here the parameters are,
Parent -dialog is centered over the parent component. if a parent is Null, the dialog is Centred on the screen.
message- message to display on a dialog box
title- display the title from the dialog box
message type - one of ERROR_MESSAGE,INFORMATION_MESSAGE,QUESTION_MESSAGE,PLAIN_MESSAGE
All message dialog types except PLAIN_MESSAGE display an icon to the left of the message. These icons provide the visual indication of the message’s importance to the user’s icon. An icon to display instead of one of the standard icons.
NOTE: A QUESTION_MESSAGE icon is the default icon for an input dialog box.
-static void showInternalMessageDialog(Component parent, object message)
-static void showInternalMessageDialog(Component parent, object message, String title, int message type)
-static void showInternalMessageDialog(Component parent, object message,String title, int message type,Icon icon)
The showInternalMessageDialog() method is used to display a message dialog or an internal message dialog.
-static int showConfirmDialog(Component parent, object message)
-static int showConfirmDialog(Components parent, object message,String title,int option type)
-static int showConfirmDialog(Component parent, object message, String title, int option type, int message type)
-static int showConfirmDialog(Component parent, object message, String title, int option type, int message type, Icon icon)
Here the parameter optionTypes are YES_NO_OPTION,OK_CANCEL_OPTION,YES_NO_CANCEL_OPTION,DEFAULT_OPTION.
The showConfirmDialog() method is used to display a confirmation dialog.Returns the option selected by the user. Options:OK_OPTION,CANCEL_OPTION,YES_OPTION,NO_OPTION
Sample Program of Joptionpane in java:
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
Public class show()
{
Public static void main(String args[])
String str=JOptionPane.showInputDialog(“enter the age”);
Int age=Integer.parseInt(str);
JOptionPane.showMessageDialog(null,”your age is”+age,”age”,JoptionPane.PLAIN_MESSAGE).
}
}