







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Types of joptionpane class and examples how to use it
Typology: Lecture notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!








JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. While the JOptionPane class may appear complex because of the large number of methods, almost all uses of this class are one-line calls to one of the static showXxxDialogshowConfirmDialog^ Asks a confirming question, like yes/no/cancel.methods shown below: showInputDialog Prompt for some input. showMessageDialog Tell the user about something that has happened. showOptionDialog The Grand Unification of the above three.
All dialogs are modal. Each showXxxDialog method blocks the current thread until the user's interaction is complete. The basic appearance of one of these dialog boxes is generally similar to the picture below, although the various look-and-feels are ultimately responsible for the final result. In particular, the look-and-feels will adjust the layout to accommodate the option pane's ComponentOrientation property. icon message input value option buttons
message A descriptive message to be placed in the dialog box. In the most common usage, message is just a String or String constant. However, the type of this parameter is actually Object. Its interpretation depends on its type: Object[] An array of objects is interpreted as a series of messages (one per object) arranged in a vertical stack. The interpretation is recursive
messageType Defines the style of the message. The Look and Feel manager may lay out the dialog differently depending on this value, and will often provide a default icon. The possible values are: (^) ERROR_MESSAGE Used for error messages (^) INFORMATION_MESSAGE Used for information messages (^) WARNING_MESSAGE Used for warning messages (^) QUESTION_MESSAGE Used for questions (^) PLAIN_MESSAGE No icon is used
options A more detailed description of the set of option buttons that will appear at the bottom of the dialog box. The usual value for the options parameter is an array of Strings. But the parameter type is an array of Objects. A button is created for each object depending on its type: (^) Component The component is added to the button row directly. (^) Icon A JButton is created with this as its label. (^) other The Object is converted to a string using its toString() method and the result is
icon A decorative icon to be placed in the dialog box. A default value for this is determined by the messageType parameter. title The title for the dialog box. initialValue The default selection (input value).
Show an error dialog that displays the message, ' alert ': JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE); Show an information panel with the options yes/no and message ' JOptionPane.showConfirmDialog(null,choose one ': "choose one", "choose one", JOptionPane.YES_NO_OPTION);
Show a warning dialog with the options OK , CANCEL , title ' Warning ', and message ' Click OK to continue ': Object[] options = { "OK", "CANCEL" }; JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
Show a dialog asking the user to select a String: Object[] possibleValues = { "First", "Second", "Third" }; Object selectedValue = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);