1. Belongs to javax.swing package.
2. Function: Customize four different types of standard dialog boxes.
ConfirmDialog Confirmation dialog box. Ask a question and let the user confirm it themselves (press the "Yes" or "No" button)
InputDialog prompts for text input
MessageDialog displays information
OptionDialog combines the other three dialog types.
3. These four dialog boxes can be displayed using showXXXDialog(). like:
showConfirmDialog() displays the confirmation dialog box,
showInputDialog() displays the input text dialog box,
showMessageDialog() displays a message dialog box,
showOptionDialog() Shows an optional dialog box.
4. Parameter description.
(1) ParentComponent: Indicates the parent window object of the dialog box, usually the current window.
It can also be null, which means the default Frame will be used as the parent window. In this case, the dialog box will be set in the center of the screen. (2) message: indicates the descriptive text to be displayed in the dialog box (3) String title: title bar text string. (4) Component: the component to be displayed in the dialog box (such as a button) (5) Icon: the icon to be displayed in the dialog box (6) messageType (icon):
ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
QUESTION_MESSAGE, PLAIN_MESSAGE, (7) optionType: button options displayed at the bottom of the dialog box.
DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, OK_CANCEL_OPTION.
5. Usage examples: (1) Display MessageDialog
JOptionPane.showMessageDialog(null, "Message content to be displayed", "Title", JOptionPane.ERROR_MESSAGE);
(2) Display ConfirmDialog
JOptionPane.showConfirmDialog( null , "message" , "title", OptionPane.YES_NO_OPTION );
(3) Display OptionDialog:
This kind of dialog box allows the user to set the number of each button and returns the sequence number of the user clicking each button (counting starts from 0)
Object[] options = {"query","deposit","withdraw","exit"};
int response=JOptionPane.showOptionDialog (null, "Select business type", "ATM cash machine", JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0] );
if (response == 0)
{JOptionPane.showMessageDialog(null,"You pressed the query button");}
else if(response == 1)
{JOptionPane.showMessageDialog(null,"You pressed the deposit button");}
else if(response == 2)
{JOptionPane.showMessageDialog(null,"You pressed the withdrawal button");}
else if(response == 3)
{JOptionPane.showMessageDialog(null,"You pressed the exit button");}
(4) Display the InputDialog to allow the user to input
String inputValue = JOptionPane.showInputDialog("Please input a value");
(5) Display the InputDialog to allow the user to selectively input
Object[] possibleValues = { "First", "Second", "Third" };
//User's selection items
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input", JOptionPane.INFORMATION_MESSAGE,
null, possibleValues, possibleValues[0]);
setTitle ("You pressed" + (String)selectedValue+"item") ;}