Introduction to JAVA “Option Panes” (dialogs) Option Panes (sometimes called dialog boxes) are pop-up boxes used to tell the user something or ask the user for input. Examples include: The first two examples are Message dialogs. They display a message and wait for the user to click OK to continue. You can easily change the message, the window title and the icon. The next two examples are Input dialogs, one with a text box and one with a pull-down menu. The text box type are very easy to create, the other only slightly more difficult. The next example is a Confirm dialog. Confirm dialogs with Yes/No, Yes/No/Cancel and OK/Cancel are very easy to create. The last two are examples of Option dialogs. Option dialogs can be used to do many different things. You can put any Java Components you want into an Option dialog. The following is part of the description from the Java API. To see the full entry, go to the Help menu, select “Help with Java class libraries”, go to the index, and find JOptionPane. public class JOptionPane extends JComponent implements Accessible JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. For information about using JOptionPane, see How to Make Dialogs, a section in The Java Tutorial. While the JOptionPane class may appear complex because of the large number of methods, almost all uses of this class are one-line calls to one of the static showXxxDialog methods shown below: Method Name Description showConfirmDialog Asks a confirming question, like yes/no/cancel. showInputDialog Prompt for some input. showMessageDialog Tell the user about something that has happened. showOptionDialog The Grand Unification of the above three. Each of these methods also comes in a showInternalXXX flavor, which uses an internal frame to hold the dialog box (see JInternalFrame). Multiple convenience methods have also been defined -- overloaded versions of the basic methods that use different parameter lists. All dialogs are modal. Each showXxxDialog method blocks the current thread until the user's interaction is complete. Notice the last sentence. “Modal” means that the main program will not continue until the dialog box has been closed; if the main program had a window, the user would not be able to go to that window. “Modeless” means that the main program would continue running at the same time as the dialog; i.e. the user could switch back and forth between them. JOptionPanes are always modal. The attached program contains some examples of how you can use JOptionPanes. (It is also saved in the Shared folder, so you can copy and paste from it.) Assignments Look at the example program below! Using Java Option Panes, complete the following tasks: 1. Write a program (Dialog1) that asks the user to type in their name and then the computer tells them to have a nice day. If the user types in Fred, then the label box would say… Have a nice day Fred. 2. Write a program (Dialog2) that asks the user for a number and then tells them the square of the number. If the user types in 12, the label box would say… The square is 144 3. Write a program (Dialog3) that asks the user for a price and then tells them the cost with 13% tax added on. If the user types in 5.00, the label box would say… Cost with tax is $5.65 4. Temperature Converter. Write a program (Dialog4) that uses an Option dialog to ask the user for a temperature and give a text field, two radio buttons (“F to C” and “C to F”) and a Convert button all on one dialog. The program should check the radio buttons to see which is selected and then convert the temperature from Fahrenheit to Celsius [(temp32)*5/9] or Celsius to Fahrenheit [temp*9/5+32] and display the answer (with the correct units – C or F). Make sure you test it! This one can be tricky. Look at the last 2 sections of the example program below. 5. Write a program (Dialog5) that asks the user to enter a mark. The program then displays PASS or FAIL depending on whether the mark was less than 50. 6. Write a program (Dialog6) that asks the user to type in two numbers. The program then states which one is largest. Example: if the user types 23 and 40, the label box would display: “The Largest is 40”. If the numbers happen to be the same it should say “They are Equal” HINTS: To convert a String to an int, use: (where value is an int and input is a String) value = Integer.parseInt(input); To convert safely (i.e. catch Strings that are not valid numbers): try { value = Integer.parseInt (input); } catch (NumberFormatException e) { value = 0; // just make it zero for simplicity } import javax.swing.*; // The "Dialogs" class. public class Dialogs { public static void main (String[] args) { // Simple Input dialog: String inputValue = JOptionPane.showInputDialog ("Please enter something:"); // Simple Message dialog: JOptionPane.showMessageDialog (null, "Your said: " + inputValue); // Confirm dialog with Yes and No buttons: int choice = JOptionPane.showConfirmDialog (null, "Choose one", "choose one", JOptionPane.YES_NO_OPTION); // Message dialog with title and icon: (ERROR icon and WARNING icon) if (choice == JOptionPane.YES_OPTION) JOptionPane.showMessageDialog (null, "You selected YES", "alert", JOptionPane.ERROR_MESSAGE); else JOptionPane.showMessageDialog (null, "You selected NO", "alert", JOptionPane.WARNING_MESSAGE); // Confirm dialog with Yes, No and Cancel buttons and INFORMATION icon: choice = JOptionPane.showConfirmDialog (null, "Please choose one", "information", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE); // Message dialogs with different icons: INFORMATION, QUESTION and PLAIN (no icon) if (choice == JOptionPane.YES_OPTION) JOptionPane.showMessageDialog (null, "You selected YES", "Response", JOptionPane.INFORMATION_MESSAGE); else if (choice == JOptionPane.NO_OPTION) JOptionPane.showMessageDialog (null, "You selected NO", "Response", JOptionPane.QUESTION_MESSAGE); else JOptionPane.showMessageDialog (null, "You selected Cancel", "Response", JOptionPane.PLAIN_MESSAGE); // Option dialog with user-defined buttons: (default value is the first in the list -- index 0) Object[] options = {"One", "Two"}; choice = JOptionPane.showOptionDialog (null, "How many?", "Important", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options [0]); if (choice == 0) JOptionPane.showMessageDialog (null, "You selected One"); else JOptionPane.showMessageDialog (null, "You selected Two"); // Input dialog with a pulldown menu: (default value is the third in the list -- index 2) Object[] possibleValues = {"First", "Second", "Third"}; Object selectedValue = JOptionPane.showInputDialog (null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues [2]); if (selectedValue == possibleValues [0]) JOptionPane.showMessageDialog (null, "You selected First"); else if (selectedValue == possibleValues [1]) JOptionPane.showMessageDialog (null, "You selected Second"); else JOptionPane.showMessageDialog (null, "You selected Third"); // Option dialog with radio buttons and a regular button (needed to continue from the dialog) JRadioButton radio1 = new JRadioButton ("Choice 1"); JRadioButton radio2 = new JRadioButton ("Choice 2", true); ButtonGroup group = new ButtonGroup (); group.add (radio1); group.add (radio2); Object[] newOptions = {radio1, radio2, "Done"}; JOptionPane.showOptionDialog (null, "Choose one", "Input", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, newOptions, newOptions [2]); if (radio1.isSelected ()) JOptionPane.showMessageDialog (null, "You selected Choice 1"); if (radio2.isSelected ()) JOptionPane.showMessageDialog (null, "You selected Choice 2"); // Option dialog with checkboxes and a regular button (needed to continue from the dialog) JCheckBox chkbox1 = new JCheckBox ("Ketchup"); JCheckBox chkbox2 = new JCheckBox ("Mustard"); JCheckBox chkbox3 = new JCheckBox ("Relish"); Object[] moreOptions = {chkbox1, chkbox2, chkbox3, "Done"}; JOptionPane.showOptionDialog (null, "Choose one", "Input", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, moreOptions, moreOptions [3]); String toppings = ""; if (chkbox1.isSelected ()) toppings = toppings+" Ketchup"; if (chkbox2.isSelected ()) toppings = toppings+" Mustard"; if (chkbox3.isSelected ()) toppings = toppings+" Relish"; JOptionPane.showMessageDialog (null, "Toppings chosen: "+toppings); } // main method } // Dialogs class