(Optional) GUI and Graphics Case Study: Using Dialog Boxes
(Optional) GUI and Graphics Case Study Using Dialog Boxes
Introduction
This case study is designed for those who want to begin learning Java's powerful capabilities for creating graphical user interfaces (GUIs) and graphics earlier in the book than the main discussions of these topics in Chapter 11, GUI Components: Part 1, Chapter 12, Graphics and Java2D, and Chapter 22, GUI Components: Part 2.
The GUI and Graphics Case Study appears in 10 brief sections (Fig. 3.16). Each section introduces a few basic concepts and provides visual, graphical examples and full source code. In the first few sections, you create your first graphical applications. In the following sections, you use the object-oriented programming concepts presented through Chapter 10 to create a drawing application that draws a variety of shapes. When we formally introduce GUIs in Chapter 11, we use the mouse to choose exactly which shapes to draw and where to draw them. In Chapter 12, we add capabilities of the Java 2D graphics API to draw the shapes with different line thicknesses and fills. We hope you will find this case study informative and entertaining.
Location |
TitleExercise(s) |
---|---|
Section 3.9 |
Using Dialog BoxesBasic input and output with dialog boxes |
Section 4.14 |
Creating Simple DrawingsDisplaying and drawing lines on the screen |
Section 5.10 |
Drawing Rectangles and OvalsUsing shapes to represent data |
Section 6.13 |
Colors and Filled ShapesDrawing a bull's-eye and random graphics |
Section 7.13 |
Drawing ArcsDrawing spirals with arcs |
Section 8.18 |
Using Objects with GraphicsStoring shapes as objects |
Section 9.8 |
Displaying Text and Images Using LabelsProviding status information |
Section 10.8 |
Drawing with PolymorphismIdentifying the similarities between shapes |
Exercise 11.18 |
Expanding the InterfaceUsing GUI components and event handling |
Exercise 12.12 |
Adding Java 2DUsing the Java 2D API to enhance drawings |
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 userssuch windows are called message dialogs. Figure 3.17 displays the string "Welcome to Java" 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 to Java" ); 11 } // end main 12 } // end class Dialog1
|
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
|
Lines 1011 use method showInputDialog of class JOptionPane to display a simple input dialog containing a prompt and a field for the user to enter text, known as a text field. The argument to showInputDialog is the prompt that indicates what the user should enter. The user types characters in the text field, then clicks the OK button or presses the Enter key to return the String to the program. Method showInputDialog returns a String containing the characters typed by the user, which we store in variable name. [Note: If you press the Cancel button in the dialog, the method returns null and the program displays the word "null" as the name.]
Lines 1415 use static String method format to return a String containing a greeting with the name entered by the user. Method format is similar to method System.out.printf, except that format returns a formatted String rather than displaying it in a command window. Line 18 displays the greeting in a message dialog.
GUI and Graphics Case Study 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 ) 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. |