Java Reference
In-Depth Information
Coding the clearFields() Method
The final method in the Reservations class is a user-defined method named
clearFields(), which clears the input fields after a successful booking. Figure 5-35
displays the code for the clearFields() method. The method accepts no passed
parameters and returns void.
136
137
//reset the text fields and choice component
138
void clearFields ()
139
{
140
nameField.setText ( "" ) ;
141
phoneField.setText ( "" ) ;
142
numberOfGuests.select ( 0 ) ;
143
nameField.requestFocus () ;
144
hidden.setState ( true ) ;
145
} //end of clearFields() method
146
147 } //end of Reservations class
FIGURE 5-35
When called from the actionPerformed() method, the clearFields() method
will reset the nameField to a null String (" ") in line 140 and reset the phoneField
in line 141. To reset the Choice component in line 142, the select() method ,
with its integer argument that represents the position in the list, is set to zero
(the first element in the list). The requestFocus() method in line 143 sets the
focus and returns the insertion point to the nameField. The hidden Checkbox
again is set to true in line 144 to clear the Nonsmoking and Smoking option
buttons on the interface.
The step below enters the code for the clearFields() method.
To Enter Code for the clearFields() Method
1. Enter the code as shown in Figure 5-35.
The TextPad window displays the code for the clearFields() method
(Figure 5-36). The closing brace in line 147 ends the Reservations class.
clearFields()
method
closing brace
for Reservations
class
FIGURE 5-36
 
Search WWH ::




Custom Search