Apr 18, 2012

Text Field

To accept line of textual data from the user AWT provides TextField. It is the subclass of TextComponent. The Constructor for TextField are listed below:

          TextField()              - It constructs an empty text field.
          TextField(String text)         - It constructs a text field with the initial content is text.
          TextField(int n)                  - Constructs an empty text field with the n number of columns.
          TextField(String text, int n) - Constructs a text field whose initial content is text with n columns.

getText()   - It returns the text content of the TextField.
setText()   - It set the text for the TextField
setEchoChar(char c) – It allows you to specify an echo character for accepting password. If you have specified “*” as echo character. It displays the “*” in the text field instead of the text the user has keyed in.


import java.applet.*;
import java.awt.*;

public class TextFieldDemo extends Applet
    public void init()
        setLayout(new GridLayout(2, 2));
        Label l=new Label("Enter the user Name",Label.CENTER);
        TextField t1=new TextField();
        Label l1=new Label("Enter your Password",Label.CENTER);
        TextField t2=new TextField(10);

 * <applet code="TextFieldDemo.class" width=250 height=50>
 * </applet>

How to Execute this Example??

d:\>  javac TextFieldDemo.java
d:\>  appletviewer TextFieldDemo.java



Post a Comment