Presentation is loading. Please wait.

Presentation is loading. Please wait.

Message, Input, and Confirm Dialogs

Similar presentations


Presentation on theme: "Message, Input, and Confirm Dialogs"— Presentation transcript:

1 Message, Input, and Confirm Dialogs
JOptionPane JOptionPane Dialogs January 17, 2019

2 Dialog Boxes Message, Input, and Confirm Dialogs JOptionPane Dialogs
January 17, 2019

3 Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input A variety of simple dialog boxes can be displayed using the JOptionPane class Message Dialog - a dialog box that displays a message Input Dialog - a dialog box that prompts the user for typed input Confirm Dialog This is a dialog box that asks the user a Yes/No question JOptionPane Dialogs January 17, 2019

4 Dialog Boxes The JOptionPane class provides static methods to display each type of dialog box JOptionPane Dialogs January 17, 2019

5 Message dialogs JOptionPane Dialogs January 17, 2019

6 Message Dialogs JOptionPane.showMessageDialog(null, "Hello World"); The first argument can be a reference to a graphical component such as another window The dialog box is displayed centered on that component If null is passed as the first argument, the dialog box to be displayed in the center of the screen The second argument is the message that is to be displayed JOptionPane Dialogs January 17, 2019

7 Last parameter determines which icon is shown here
Message Dialogs By default the dialog box has: the string “Message” displayed in its title bar, and an information icon (showing the letter “i”) is displayed JOptionPane.showMessageDialog(null, "Invalid Data", "My Message Box", JOptionPane.ERROR_MESSAGE); The third parameter is the title bar text Last parameter determines which icon is shown here JOptionPane Dialogs January 17, 2019

8 Message Dialogs These constants can be used to control the icon that is displayed JOptionPane.ERROR_MESSAGE JOptionPane.INFORMATION_MESSAGE JOptionPane.WARNING_MESSAGE JOptionPane.QUESTION_MESSAGE JOptionPane.PLAIN_MESSAGE JOptionPane Dialogs January 17, 2019

9 Message Dialogs with Icons
JOptionPane Dialogs January 17, 2019

10 Message Dialogs The dialog boxes displayed by the JOptionPane class are modal dialog boxes A modal dialog box suspends execution of any other instructions in your program until the dialog box is closed When the JOptionPane.showMessageDialog method is called, the instructions after the method call do not execute until the user closes the message box JOptionPane Dialogs January 17, 2019

11 Input Dialog JOptionPane Dialogs January 17, 2019

12 Input Dialogs An input dialog is a quick and simple way to ask the user to enter String data The dialog displays a text field, an OK button and a Cancel button If OK is pressed, the dialog returns the user’s input If Cancel is pressed, the dialog returns null JOptionPane Dialogs January 17, 2019

13 Input Dialogs The JOptionPane has several overloaded versions of the static showInputDialog method Here are two of them: showInputDialog(Object message) showInputDialog(Component parent, Object message, String title, int messageType) JOptionPane Dialogs January 17, 2019

14 Input Dialogs String name; name = JOptionPane.showInputDialog("Enter your name."); The argument passed to the method is the message to display If the user clicks on the OK button, name references the string entered by the user If the user clicks on the Cancel button, name references null JOptionPane Dialogs January 17, 2019

15 Input Dialogs By default, the input dialog box:
has the string “Input” in its title bar, and displays a question icon String value; value = JOptionPane.showInputDialog (null, "Enter the value again.", "Enter Carefully!", JOptionPane.WARNING_MESSAGE); JOptionPane Dialogs January 17, 2019

16 Input Dialog with Default Value
Occasionally with an Input Dialog, a particular value is entered frequently In that case, that value can be used as a default value the user can accept or replace The calling sequence in this case is JOptionPane.showInputDialog (message, defaultValue) Example: JOptionPane Dialogs January 17, 2019

17 Numeric Data Type Wrappers
The Input Dialog inputs only string data. If one inputs a number, it must be converted from the string received into a number before it can be used in numeric calculations Java provides wrapper classes for all primitive data types The numeric primitive wrapper classes are: Wrapper Class Numeric Primitive Type It Applies To Byte byte Double double Float float Integer int Long long Short short JOptionPane Dialogs January 17, 2019

18 The Parse Methods A string containing a number, such as “127.89”, can be converted to a numeric data type Each of the numeric wrapper classes has a static method that converts a string to a number The Integer class has a method that converts a String to an int The Double class has a method that converts a String to a double The other numeric wrapper classes have similar methods These methods are known as parse methods because their names begin with the word “parse” JOptionPane Dialogs January 17, 2019

19 The Parse Methods: Examples
// Store 1 in bVar byte bVar = Byte.parseByte("1"); // Store 2599 in iVar int iVar = Integer.parseInt("2599"); // Store 10 in sVar short sVar = Short.parseShort("10"); // Store in longVar long longVar = Long.parseLong("15908"); // Store 12.3 in fVar float fVar = Float.parseFloat("12.3"); // Store in dVar double dVar = Double.parseDouble("7945.6"); The parse methods all throw a NumberFormatException if the String argument does not represent a numeric value JOptionPane Dialogs January 17, 2019

20 Now, one can use hours and sales in calculations
“Parse” examples Now, one can use hours and sales in calculations JOptionPane Dialogs January 17, 2019

21 Confirm dialog JOptionPane Dialogs January 17, 2019

22 Confirm Dialog A confirm dialog box typically asks the user a yes or no question By default Yes, No, and Cancel buttons are displayed The showConfirmDialog method is used to display a confirm dialog box There are several overloaded versions of this method int showConfirmDialog(Component parent, Object message) int showConfirmDialog(Component parent, Object message, String title, int optionType) JOptionPane Dialogs January 17, 2019

23 Confirm Dialog By default the confirm dialog box displays:
int value; value = JOptionPane.showConfirmDialog (null, "Are you sure?"); By default the confirm dialog box displays: “Select an Option” in its title bar Yes, No, and Cancel buttons JOptionPane Dialogs January 17, 2019

24 Confirm Dialog The showConfirmDialog method returns an integer that represents the button clicked by the user The button that was clicked is determined by comparing the method’s return value to one of the following constants: JOptionPane.YES_OPTION JOptionPane.NO_OPTION JOptionPane.CANCEL_OPTION JOptionPane Dialogs January 17, 2019

25 Confirm Dialog int value; value = JOptionPane.showConfirmDialog (null,
"Are you sure?"); if (value == JOptionPane.YES_OPTION) { // If the user clicked Yes, this code is executed } else if (value == JOptionPane.NO_OPTION) // If the user clicked No, this code is executed else if (value == JOptionPane.CANCEL_OPTION) // If the user clicked Cancel, this code is executed JOptionPane Dialogs January 17, 2019

26 Confirm Dialog int value; value = JOptionPane.showConfirmDialog(null, "Are you sure?", "Please Confirm", JOptionPane.YES_NO_OPTION); One of the following constants can be used for the fourth parameter to control which buttons appear in the Confirm dialog JOptionPane.YES_NO_OPTION JOptionPane.YES_NO_CANCEL_OPTION Example: TestAverageDialog.java JOptionPane Dialogs January 17, 2019


Download ppt "Message, Input, and Confirm Dialogs"

Similar presentations


Ads by Google