Creating and Using Dialogs
- A dialog is a box that pops up and prompts the user for a value or informs them of something
- One way: directly create objects of type JDialog
Using JDialog
- JDialog, like JFrame, JWindow, and JApplet, uses a content pane to organize its contents
- If you create a JDialog directly, you are responsible for:
- Laying out the dialog components (BorderLayout by default):
- Messages and Icons
- Input areas and Buttons (Yes, No, Ok, Cancel, etc.)
- Adding listeners to the dialog components
- Showing and hiding the dialog when appropriate
Another Way: JOptionPane
- The JOptionPane class has a number of static methods that do the work of creating, laying out, and adding listeners for dialogs that perform standard functions
- The methods are of the form showXXXDialog where XXX is:
- Message: Tell the user about something that has happened
- Confirm: Ask a confirming question, like yes/no/cancel
- Input: Prompt for some input
- Option: Combination of the above
General Dialog Layout
- Note: the dialogs created by the JOptionPane class are modal, that is, processing is blocked until user interaction is complete
Arguments Accepted by showXXXDialog Methods
- Parent component: used to position dialog; if null then dialog is centered on screen
- Message: a descriptive message to be displayed in the dialog box, often a String
- Message type: coded integer specifying the style of the message:
- JOptionPane.ERROR_MESSAGE
- JOptionPane.INFORMATION_MESSAGE
- JOptionPane.QUESTION_MESSAGE
- JOptionPane.WARNING_MESSAGE
- JOptionPane.PLAIN_MESSAGE
Arguments Accepted by showXXXDialog Methods (cont'd)
- Title: title to appear on dialog's title bar
- Button options: coded integer defining the set of available response buttons:
- JOptionPane.OK_CANCEL_OPTION
- JOptionPane.YES_NO_OPTION
- JOptionPane.YES_NO_CANCEL_OPTION
- JOptionPane.DEFAULT_OPTION
- Icon: icon to be displayed by message. If null, a default is used depending on Message type and Look-and-Feel
Arguments Accepted by showXXXDialog Methods (cont'd)
- Selection values: an array of Objects, often Strings, to be selected from when getting input from user
- Initial selection value: value from Selection values to be selected by default
An Example Dialog Task
- Put up a dialog that:
- Displays an icon indicating this is a dialog that is transmitting information
- Displays the message ''A CATASTROPHIC ERROR HAS OCCURRED''
- Displays a single button labeled ''OK''
- Adds a listener to the button that causes the dialog to disappear when the button is clicked, allowing processing to continue
A Simple Message Dialog
Notes On The Example
- As a static method, showMessageDialog must be fully qualified by the class name
- As a void method, showMessageDialog is called for side effect
- The first argument is the dialog's parent component. If null, the dialog is centered on the screen
- The second argument is an Object to be displayed, often a String
- Output:
Dialogs That Ask Questions
- The showConfirmDialog method can present more than one button, from among Yes, No, OK, and Cancel
- A symbolic integer is returned indicating which button was clicked:
- JOptionPane.CANCEL_OPTION
- JOptionPane.YES_OPTION
- JOptionPane.NO_OPTION
- JOptionPane.OK_OPTION
- Use this value to determine the course of action to take after interaction with the user
showConfirmDialog Example
Changing the Dialog Title and Buttons
- All showXXXDialog methods have multiple versions allowing different combinations of arguments
- Suppose you want only the OK and Cancel buttons, and a non-default dialog title:
Changing the Dialog Title and Buttons (cont'd)
Getting Textual Input
- showInputDialog can return a string that is input by the user
- There are many argument combinations. Here is the simplest:
Getting List Selections
Getting List Selections (cont'd)