Java Reference
In-Depth Information
54
55
//This method is triggered by the user clicking an option button
56
public void itemStateChanged ( ItemEvent choice )
57
{
58
try
59
{
60
dollars = getSales () ;
61
empCode = getCode () ;
62
answer = getComm ( dollars,empCode ) ;
63
output ( answer, dollars ) ;
64
}
65
66
catch ( NumberFormatException e )
67
{
68
outputLabel.setText ( "You must enter a dollar amount greater than zero." ) ;
69
hiddenBox.setState ( true ) ;
70
salesField.setText ( "" ) ;
71
salesField.requestFocus () ;
72
}
73
}
FIGURE 4-50
Lines 58 through 64 show the try statement and the braces enclosing the
statements to call the getSales() method that returns a sales amount, the
getCode() method that returns a commission code, the getComm() method that
calculates a commission, and the output() method used to format and display
the output. The try statement is similar to the main() method in the application,
except it does not have to call the finish() method to terminate the program.
Applets are controlled by their calling programs; therefore, Applet Viewer's Close
button will be used to close the applet.
The catch statement, which executes when the try statement encounters an
exception, uses the setText() method to display an error message. The message is
displayed in line 68 in the outputLabel component on the applet interface. The
setState() method in line 69 is used to set the state of the hidden Checkbox to
true, thus deselecting the other Checkboxes. The setText() method in line 70 is
used to clear the salesField TextField. Finally, in line 71, the requestFocus()
method moves the insertion point to the salesField TextField so that the user can
reenter a value for the sales amount.
The following step codes the try and catch statements for the applet. As you
enter the code for this and the remaining methods in the applet, you may use
cutting and pasting techniques to copy appropriate code from the application to
the applet.
Search WWH ::




Custom Search