To simplify user interaction and make data entry easier, you can use java controls. Controls are components, such as buttons, labels and text boxes, that can be added to containers like frames, panels and applets. The Java.awt package provides an integrated set of classes to manage user interface components.
The text Field allows you to specify an echo character that helps you accept a password without displaying what the user has keyed in. For example, say you have specified “*” as the echo character, and the user type in five character, “*****” is displayed in the text filed instead of the text the user has keyed in. To do this, first create the text field itself; then use the setEchoCharacter() method to set the character that is echoed on the screen. Here is an example:
TextField tf = new TextField(30);tf.setEchoCharacter('*');
The Applet in Example7 below creates several text fields. Labels are used to identify the fields. One of the fields uses an obscuring character to hide text being input. This applet uses the default layout manager, the only thing causing the six components to appear in three different lines is the width of the window.
After creating the controls, we initialize them using the “new” keyword. Use the add() method to add controls to the container, in this case the applet. The other methods for TextField are:
Method
Action
String getText()
Extracts the text from the text field (as a string).
Void setText(String str)
Sets the text of the text field to the text specified in the str.
Int getColumns()
Returns the width of this text field.
select(int, int)
Selects the text between the two integer positions (positions start from 0)
selectAll()
Selects all the text in the field
Boolean isEditable ()
Returns true or false based on whether the text is editable
Char getEchoChar()
Returns the character used for masking input
Boolean echoCharIsSet(char c)
Returns true or false based on whether the field has a masking character