Java Reference
In-Depth Information
Writing Data in the BillPayer Program
Figure 8-25 displays the actionPerformed() method. Line 173 calls a method
named checkFields(), which will be coded later in this chapter. If the checkFields()
method returns a true value, the program writes the data to the data file (lines 176
through 184). The Box 4 message box from Table 8-2 on page 491 is coded in line
186. If an error occurs during the write process, the system exits with an error code
of 1 in line 190, and the data file closes automatically. The try and catch structure
ensures that all errors are handled by the program as required by Java.
169
public void
actionPerformed
(
ActionEvent
e
)
170
{
171
String
arg = e.getActionCommand
()
;
172
173
if
(
checkFields
())
174
{
175
try
176
{
177
output.writeUTF
(
acctNum.getText
())
;
178
output.writeUTF
(
pmt.getText
())
;
179
output.writeUTF
(
firstName.getText
())
;
180
output.writeUTF
(
lastName.getText
())
;
181
output.writeUTF
(
address.getText
())
;
182
output.writeUTF
(
city.getText
())
;
183
output.writeUTF
(
state.getText
())
;
184
output.writeUTF
(
zip.getText
())
;
185
186
JOptionPane.showMessageDialog
(
null
,
"The payment information has been saved."
,
"Submission Successful"
,JOptionPane.INFORMATION_MESSAGE
)
;
187
}
188
catch
(
IOException
c
)
189
{
190
System
.exit
(
1
)
;
191
}
192
clearFields
()
;
193
}
194
}
FIGURE 8-25
The following step codes the actionPerformed() method.
To Code the actionPerformed() Method
1.
Enter lines 171
through 193 as
shown in
Figure 8-25.
TextPad displays
the new code
(Figure 8-26). The
action event writes
the data to the
data file when the
user clicks the
Submit button.
writeUTF()
methods
code to exit program
FIGURE 8-26
code to display
message box
Search WWH ::
Custom Search