Three text fields, nameField, passField, resultField and three labels, lab1, lab2, lab3 are created.
The text field passField is registered with ActionListener using addActionListener() method of TextField. Pressing Enter key in passField generates an event but pressing Enter key in nameField and resultField do not have any event handling as they are not registered with ActionListener (as not required in the program).
passField is meant for entering password. Password should be secret and for this reason passField characters entered by the user are echoed (displayed) as *. resultField is not allowed by the user to enter any text as it is meant for the programmer to display the result.
String str1 = nameField.getText();
Two methods exist in TextField used very often – getText() and setText(). getText() returns the text entered by the user in the text field and setText() method places the text in the text field.
Another program on text field is given Java TextField – Setting Properties where properties for the text field are given with different methods of TextField. For practice, a text field with applet is given in TextField Applet – Adding Two Numbers. The frame you get do not close when clicked over the close icon on the title bar of the frame. It requires extra code close icon to work.