Title: Creating and Using Dialogs
1Creating 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
Object
Component
Container
Window
Dialog
JDialog
2Using 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
3Another 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
4General Dialog Layout
Message
Icon
Input Value
Option Buttons
- Note the dialogs created by the JOptionPane
class are modal, that is, processing is blocked
until user interaction is complete
5An 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
6A Simple Message Dialog
import javax.swing. public class DialogTest
public static void main(String args)
JOptionPane.showMessageDialog(
null, "A CATASTROPHIC ERROR HAS
OCCURRED.") System.exit(0)
7Notes 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
8Dialogs 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
9showConfirmDialog Example
... int response
JOptionPane.showConfirmDialog(
null, "DO YOU WANT TO
CONTINUE?") if (response
JOptionPane.YES_OPTION) ... // user
clicked YES else if (response
JOptionPane.NO_OPTION) ... //
user clicked NO else
... // user clicked CANCEL
...
10Changing 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
11Changing the Dialog Title and Buttons (cont'd)
... int response JOptionPane.showConfirmDia
log( null, "DO YOU WANT TO
CONTINUE?", "Continue Dialog", // Dialog
title JOptionPane.OK_CANCEL_OPTION) if
(response JOptionPane.OK_OPTION) ... //
user clicked OK else ... // user clicked
CANCEL
12Getting Textual Input
- showInputDialog can return a string that is input
by the user - There are many argument combinations. Here is
the simplest
... String ssn JOptionPane.showInputDialog(
"ENTER YOUR SOCIAL SECURITY NUMBER") ...
13Getting List Selections
Suppose you want a dialog to present the user
with a list of possible selections
When the user clicks on the input area, a
drop-down list appears
14Getting List Selections (cont'd)
This requires a 7-argument call to
showInputDialog
... String message "Select your
favorite cereal" String title "Cereal
Selection" Object selectionValues
"Corn Flakes", "Wheaties",
"Cheerios" Object response
JOptionPane.showInputDialog(
null, message,
title, JOptionPane.QUESTION_MES
SAGE, // message type null,
// icon
selectionValues, // selections
selectionValues0) //
initially
// selected String favoriteCereal
(String)response ...