Optional Lab assignment:

Pg: 104 3.9. (Optional) GUI and Graphics Case Study: Using Dialog Boxes

Exercise:

3.1 Modify the addition program in Fig. 2.7 to use dialog-based input with JOptionPane instead of console-based input using Scanner. Since method showInputDialog only returns a String, you must convert the String the user enters to an int for use in calculations. Method [ Integer.parseInt( String s ), this is a very important method for you to use.] takes a String argument representing an integer (e.g., the result of JOptionPane.showInputDialog) and returns the value as an int. If the String does not contain a valid integer, then the program will terminate with an error.

Fig: 2.7 // Fig. 2.7: Addition.java // Addition program that displays the sum of two numbers. import java.util.Scanner; // program uses class Scanner

public class Addition { // main method begins execution of Java application public static void main( String args[] ) { // create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); int number1; // first number to add int number2; // second number to add int sum; // sum of number1 and number2

System.out.print( "Enter first integer: " ); // prompt number1 = input.nextInt(); // read first number from user

System.out.print( "Enter second integer: " ); // prompt number2 = input.nextInt(); // read second number from user

sum = number1 + number2; // add numbers

System.out.printf( "Sum is %d\n", sum ); // display sum

} // end method main

} // end class Addition

This class uses Scanner class to read two integers and perform addition operation. This is the code that you have to change to do exercise 3.1.

For those who do not have their books here is the section (Use it for your reference). If you don’t understand some of the things like import JoptinoPane it’s ok. You need it, so use it. What you are to pay attention to is the objects and methods used. I have highlighted some of things you will need to just use to display a text using JOptionPane.

Displaying Text in a Dialog Box

Although the programs presented in this book thus far display output in the command window, many Java applications use windows or dialog boxes (also called dialogs) to display output. For example, World Wide Web browsers such as Netscape or Microsoft Internet Explorer display Web pages in their own windows. E-mail programs allow you to type and read messages in a window. Typically, dialog boxes are windows in which programs display important messages to the user of the program. Class JOptionPane provides prepackaged dialog boxes that enable programs to display windows containing messages to users—such windows are called message dialogs. Figure 3.17 displays the string "Welcome\nto\nJava" in a message dialog.

Figure 3.17. Using JOptionPane to display multiple lines in a dialog box.

1 // Fig. 3.17: Dialog1.java 2 // Printing multiple lines in dialog box. 3 import javax.swing.JOptionPane; // import class JOptionPane 4 5 public class Dialog1 6 { 7 public static void main( String args[] ) 8 { 9 // display a dialog with the message 10 JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); 11 } // end main 12 } // end class Dialog1

[Page 106]

Line 3 indicates that our program uses class JOptionPane from package javax.swing. This package contains many classes that help Java programmers create graphical user interfaces (GUIs) for applications. GUI components facilitate data entry by a program's user, and formatting or presenting data outputs to the user. In method main, line 10 calls method showMessageDialog of class JOptionPane to display a dialog box containing a message. The method requires two arguments. The first argument helps the Java application determine where to position the dialog box. When the first argument is null, the dialog box appears in the center of the computer screen. The second argument is the String to display in the dialog box.

Method showMessageDialog is a special method of class JOptionPane called a static method. Such methods often define frequently used tasks that do not explicitly require creating an object. For example, many programs display messages to users in dialog boxes. Rather than require programmers to create code that performs this task, the designers of Java's JOptionPane class declared a static method for this purpose. Now, with a simple method call, all programmers can make a program display a dialog box containing a message. A static method typically is called by using its class name followed by a dot (.) and the method name, as in

ClassName.methodName( arguments )

Chapter 6, Methods: A Deeper Look will cover calling static methods in greater detail.

Entering Text in a Dialog Box

Our next application (Fig. 3.18) demonstrates input using dialogs. This program uses another predefined dialog box from class JOptionPane called an input dialog that allows the user to enter data for use in the program. The program asks for the user's name and responds with a greeting containing the name entered by the user.

Figure 3.18. Obtaining user input from a dialog.

(This item is displayed on pages 106 - 107 in the print version)

1 // Fig. 3.18: NameDialog.java 2 // Basic input with a dialog box. 3 import javax.swing.JOptionPane; 4 5 public class NameDialog 6 { 7 public static void main( String args[] ) 8 { 9 // prompt user to enter name 10 String name = 11 JOptionPane.showInputDialog( "What is your name?" ); 12 13 // create the message 14 String message = 15 String.format( "Welcome, %s, to Java Programming!", name ); 16 17 // display the message to welcome the user by name 18 JOptionPane.showMessageDialog( null, message ); 19 } // end main 20 } // end class NameDialog