1 / 17

Creating and Using Dialogs

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.

nealward
Download Presentation

Creating and Using Dialogs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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); } }

  10. 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:

  11. 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

  12. 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 } ...

  13. 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:

  14. 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 }

  15. 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"); ...

  16. 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:

  17. 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; ...

More Related