170 likes | 192 Views
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. Object. Component. Container. Window. Dialog. JDialog. Using JDialog.
E N D
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 Object Component Container Window Dialog 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 Icon Message • Note: the dialogs created by the JOptionPane class are modal, that is, processing is blocked until user interaction is complete Input Value Option Buttons
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 import javax.swing.*; public class DialogTest { public static void main(String[] args) { JOptionPane.showMessageDialog( null, "A CATASTROPHIC ERROR HAS OCCURRED."); System.exit(0); } }
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, andCancel • 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 ... 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 } ...
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) ... int response = JOptionPane.showConfirmDialog( 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 }
Getting 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"); ...
Getting 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:
Getting 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_MESSAGE, // message type null, // icon selectionValues, // selections selectionValues[0]); // initially // selected String favoriteCereal = (String)response; ...