Java Reference
In-Depth Information
If any of the text boxes are empty, the method returns false to the calling
method (line 208). Otherwise, it returns true (line 212), and the program con-
tinues with the actionPerformed() method in order to write the data.
Adding a Method to Clear the Fields and Return the Focus
The clearFields() method is a user-defined method that clears all of the
TextFields in the BillPayer interface window and returns the focus to the first
field, Account Number. The clearField() method is called after the user clicks the
Submit button and the data is written to the data file.
Figure 8-29 displays the code for the clearFields() method. Lines 219 through
226 set each field's text to a null, or empty, string (""). Line 227 then moves the
insertion point to the first field, the Account Number field, with the
requestFocus() method.
215
216
public void clearFields ()
217
{
218
//Clear fields and reset the focus
219
acctNum.setText ( "" ) ;
220
pmt.setText ( "" ) ;
221
firstName.setText ( "" ) ;
222
lastName.setText ( "" ) ;
223
address.setText ( "" ) ;
224
city.setText ( "" ) ;
225
state.setText ( "" ) ;
226
zip.setText ( "" ) ;
227
acctNum.requestFocus () ;
228
}
FIGURE 8-29
The following step codes the clearFields() method.
To Code the clearFields() Method
1. Enter the code in Figure 8-29.
TextPad displays the code for the clearFields() method (Figure 8-30).
code to clear text boxes
code to position insertion point
FIGURE 8-30
 
Search WWH ::




Custom Search