8.4 Entering Data Using Fields in a Frame | 395
the same word to mean two different things. When discussing
fields, we must clearly indicate which kind we mean.
Syntactically, Java doesn’t confuse them because a data entry
field is an object, and a field in a class is a class member.
The user clicks within a data entry field to position the
cursor there and then types a value into the field using the
keyboard. In some cases, the application may display a field
with an initial value that the user can delete before entering
a value. Such an initial value is called a default value.
Alternatively, an application can be written to display a field
without a default value. Including a default value is one way
to show users how they should type their data within the field.
A user can enter the data into the fields in any order and then go back to any field and
correct a mistyped value. None of the user’s actions are seen by the code until the user clicks
a button. Of course, the computer responds to the user’s keystrokes and mouse clicks, so some
code must be handling these actions. What is it? The JVM, working together with the oper-
ating system, handles the individual keystrokes and mouse actions.
The value that the user sees in a field on the screen is stored in the corresponding ob-
ject. We can retrieve this value at any time with the instance method getText. Of course, we
don’t want to get the value at just any old time—we want to get it when the user has finished
editing it. The user can indicate this fact by clicking on a button.
When the button event fires, the handler within the button listener is invoked. Within
the event handler is our code that gets and processes the values that are currently stored in
the field objects. After performing that task, our event handler code might restore the val-
ues in the fields to their default values (using setText, just as with a label). Seeing
the fields return to their default values signals the user that he or she can enter
another set of values. Figure 8.7 shows this process, which you should recognize
as an event loop.
Entering data in the manner just described is typical of modern applications
and is called a dialog. In a theatrical play, a dialog is an exchange between two char-
acters. In the case of the computer, a dialog is an exchange between the user and
an application. The application initiates the dialog by displaying a frame, the user
replies by entering data into the fields of the frame and clicking a button, the ap-
plication processes the data and responds by updating the frame, and the user
replies again. This dialog continues until the user indicates that there are no more data to
enter, perhaps by closing the window.
You should always take time to design a dialog so that it is easy to use. For example, the
dialog should clearly label data entry fields and indicate the form of the data to be entered
into them. Such considerations are especially important when a dialog involves entering
data into multiple fields in a frame.
Figure 8.6 Possible Data Entry Frame for a Payroll
Program
Dialog A user interface tech-
nique in which the user enters
data and then performs a sepa-
rate action (such as clicking a
button) when the entered val-
ues are ready to be processed by
the application