TextField User Name Password Validation



Three text fields, nameField, passField, resultField and three labels, lab1, lab2, lab3 are created.

passField.addActionListener(this);

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.setEchoChar(‘*’);
resultField.setEditable(false);

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();
resultField.setText(“VALID”);

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.

7 thoughts on “TextField User Name Password Validation”

  1. I have a question , if I enter any value in textfied then I want another value will be come in another textfield based on previous text field. using awt frame in java
    and another question is how to add print facility on a button click .
    i have done printing work but it give low quality print ..

Leave a Comment

Your email address will not be published.