by Dinesh Thakur Category: AWT and Applets

The AWT provides java.awt.TextField class through the component box input text or just text box. This component enables the editing and input a single line of text in a very convenient way for the user.Hence, this control is called editable control. Any data entered through this component is treated primarily as text, should be explicitly converted to another type if desired.

The TextField control is a sub-class of the TextComponent class. It can be created using the following constructors:

TextField text=new TextField( );
TextField text=new TextField(int numberofchar);
TextField text=new TextField(String str);
TextField text=new TextField(String str, int numberolchar);

The first constructor creates the default TextField. The second creates a TextField of size specified by the number of characters. The third type creates a text field with the default constructor. The text field control has the methods getText() and set Text(String str) for getting and setting the values in the text field. The text field can be made editable or non-editable by using the following methods:
setEditable(Boolean edit);
The isEditable() method returns the boolean value true if the text field is editable and the value false if the text field is non-editable. Another important characteristic of the text field is that the echo character can be set, while entering password-like values. To set echo characters the following method is called:
void setEchoCharacter(char ch)
The echo character can be obtained and it can be checked it the echo character has been set using the following methods:
boolean echoCharlsSet();
Since text field also generates the ActionEvent, the ActionListener interface can be used to handle any type of action performed in the text field. The selected text within the text field can be obtained by using the TextField Interface.

The java.awt.TextField class contains, among others, the following constructors and methods:
Following is a table that lists some of the methods of this class:




Constructs new text box with no content.


Constructs new text box with content given.

TextField(String, int)

Constructs new text box with content and given the specified width in columns.


Registers a listener class (processor events) ActionListener for the TextField.


Verifies that character echo is triggered.


Gets the current number of columns.


Obtain current character echo.




specifies the number of columns.


Specifies character echo.


import java.awt.*;

import java.awt.event.*;
public class TextFieldJavaExample extends Frame
       TextField textfield;
           setLayout(new FlowLayout());
           textfield = new TextField("Hello Java", 12);
           addWindowListener(new WindowAdapter(){
           public void windowClosing(WindowEvent we)
       public static void main(String argc[])
                    Frame frame = new TextFieldJavaExample();
                    frame.setTitle(" TextField in Java Example");
                    frame.setSize(320, 200);

TextField in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles