Page 701 - Introduction to Programming with Java: A Problem Solving Approach
P. 701

                {
}
x = -1;
// indicates an invalid x
16.13 Dialog Boxes and the JOptionPane Class 667
x is the user’s entry after it’s been converted to an integer. If x is negative, the program displays undefined in the xfBox component.
Now for the non-integer input case. Note this code near the top of the ActionPerformed method: try
{
}
x = Integer.parseInt(xBox.getText());
catch (NumberFormatException nfe)
The Integer.parseInt method attempts to convert xBox’s user-entered value to an integer. If xBox’s user-entered value is a non-integer, then parseInt throws a NumberFormatException. To handle that possibility, we put the Integer.parseInt method call inside a try block, and we include an as- sociated catch block. If parseInt throws an exception, we want to display undefined in the xfBox component. To do that, we could call xfBox.setText("undefined") in the catch block, but then we’d have redundant code—xfBox.setText("undefined") in the catch block and also in the sub- sequent if statement. To avoid code redundancy and its inherent maintenance problems, we assign 􏰀1 to x in the catch block. That causes the subsequent if statement to be true and that in turn causes xfBox. setText("undefined") to be called.
After validating the input, the actionPerformed method calculates the factorial. It first takes care Apago PDF Enhancer
of the special case when x equals 0 or 1. It then takes care of the x 􏰁 2 case by using a for loop. Study the
    code. It works fine, but do you see a way to make it more compact? You can omit the block of codethatstartswithif (x == 0||x == 1)becausethatcaseishandledbytheelse block.Morespecifically,youcandeletethesixlinesabovethesecondxf = 1;line.
16.13 Dialog Boxes and the JOptionPane Class
Write compact code.
   A dialog box—often referred to simply as a dialog—is a specialized type of window. The primary differ- ence between a dialog box and a standard window is that a dialog box is more constrained in terms of what it can do. While a standard window usually remains on the user’s screen for quite a while (often for the duration of the program) and performs many tasks, a dialog box remains on the screen only long enough to perform one specific task. While a standard window is highly customizable, a dialog box typically is locked into one particular format.
User Interface
There are three types of JOptionPane dialogs—a message dialog, an input dialog, and a confirmation dialog. Each type performs one specific task. The message dialog displays output. The input dialog displays a question and an input field. The confirmation dialog displays a yes/no question and yes/no/cancel button options. See what the different types look like in Figure 16.9. In this chapter, we’ll focus on just one of the three dialogs—the message dialog. If you want to learn about the input dialog, see the GUI track section in Chapter 3. If you want to learn about the confirmation dialog, refer to Sun’s Java API Web site.















































































   699   700   701   702   703