by Dinesh Thakur

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);
isEditable()
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:
getEchoCharacter();
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:

Method

Description

TextField()

Constructs new text box with no content.

TextField(String)

Constructs new text box with content given.

TextField(String, int)

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

addActionListener(ActionListener)

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

echoCharIsSet()

Verifies that character echo is triggered.

getColumns()

Gets the current number of columns.

getEchoChar()

Obtain current character echo.

SetColumns(int)

 

setEchoChar(char)               

specifies the number of columns.

 

Specifies character echo.

 

import java.awt.*;

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

TextField in Java Example