Open In App

Java AWT TextField

Last Updated : 26 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Java Abstract Window Toolkit (AWT) is a library for building interactive Graphical User Interfaces (GUIs) in Java Applications. In AWT, TextField is a text component that lets users add a single line of text and edit it further.

TextField in Java AWT

The TextField class in Java AWT is a GUI component in the java.awt’ package. It allows the user to enter a single line of text as an input. It is a simple way to collect text-based information from the user. You can use the TextField component to create various input fields for username, password, search boxes, etc.

Whenever a key is pressed or typed into the input field, an event is sent to the TextField. The registered KeyListener then receives the KeyEvent. ActionEvent can also be used for this purpose. If the ActionEvent is configured on the text field, then it will be triggered when the Return key is pressed. The ActionListener interface handles the event.

Syntax of Java AWT TextField

public class TextField extends TextComponent  

Class Constructors of AWT TextField

  1. TextField(): Constructs a new text field component
  2. TextField(int columns): Constructs a new empty text field with the required number of columns as the parameter.
  3. TextField(String text): Creates a new text field initialized with the string text to be displayed.
  4. TextField(String text, int columns): Creates a new text field with the given text to be displayed and a width that fits the specified number of columns.

Class Methods of AWT TextField

A list of all the methods associated with TextField with its description is mentioned below in the table:

Method

Description

void addActionListener(ActionListener l)

This action listener is added to receive action events from this text field.

void addNotify()

Creates a peer for the TextField.

boolean echoCharIsSet()

Check if there is a character set for echoing in this text field.

AccessibleContext getAccessibleContext()

Retrieves the AccessibleContext associated with the TextField.

ActionListener[] getActionListeners()

An array of all action listeners registered on a textfield is returned.

int getColumns()

Returns the number of columns in a text field.

char getEchoChar()

Returns the character to be used for echoing.

Dimension getMinimumSize()

It returns the minimum dimensions for a text field.

Dimension getMinimumSize(int columns)

Fetches the minimum dimensions for a text field with the specified number of columns.

Dimension getPreferredSize()

Returns the preferred size of a text field.

Dimension getPreferredSize(int columns)

It returns the preferred size of a text field with specified number of columns.

protected String paramString()

Returns a string indicating the state of this TextField.

protected void processActionEvent(ActionEvent e)

Action events that occur on the text field are processed by dispatching them to any registered ActionListener objects.

protected void processEvent(AWTEvent e)

Processes an event on the text field.

void removeActionListener(ActionListener l)

It removes the specified action listener from the text field..

void setColumns(int columns)

Defines the number of columns in the text field.

void setEchoChar(char c)

Defines echo character for the text field.

void setText(String t)

Sets the text that is presented by this text component to be the specified text.

Inherited Methods

The Methods included with AWT TextField are inherited by:

  • java.awt.TextComponent
  • java.lang.Component
  • java.lang.Object

Deomnstration of AWT TextField

Given below are some examples which will help you to easily understand about the TextField component.

Example 1:

Java




// Java AWT Program to demonstrate
// Use of TextField
import java.awt.*;
  
// Driver Class
public class Main {
      // main function
    public static void main(String[] args){
        
        // Create a frame
        Frame f = new Frame("TextField Example");
  
        // Create a text field
        TextField t1 = new TextField();
        
          // Set position
        t1.setBounds(50, 50, 200, 30);
        TextField t2 = new TextField();
        t2.setBounds(50, 100, 200, 30);
  
        // Add text fields to the frame
        f.add(t1);
        f.add(t2);
  
        // Set frame size
        f.setSize(300, 200);
  
        // Set frame layout
        f.setLayout(null);
  
        // Make the frame visible
        f.setVisible(true);
    }
}


Run the code using the following commands:

javac Main.java
java Main

Output:

Output of AWT TextField Example

Final Output Created:

TextField Created

Example 2:

Java




// Java AWT Program to implement
// TextField to Take input from user
import java.awt.*;
import java.awt.event.*;
  
// Driver Class
public class Main2 {
      // main function
    public static void main(String[] args)
    {
        // Create a frame
        Frame f = new Frame("TextField Example");
  
        // Create a button
        Button b = new Button("Submit");
        // Set the position of the button
        b.setBounds(160, 100, 100, 30);
  
        // Create a text field
        TextField t = new TextField();
        
        // Set position of text field
        t.setBounds(160, 50, 200, 20);
  
        // Create labels
        Label l1 = new Label("Name");
        Label l2 = new Label(" ");
        
        // Set position of labels
        l1.setBounds(40, 55, 100, 14);
        l2.setBounds(150, 170, 400, 14);
  
        // Add the components to the frame
        f.add(b);
        f.add(t);
        f.add(l1);
        f.add(l2);
  
        // Add action listener
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                l2.setText("Name: " + t.getText());
            }
        });
  
        f.setSize(400, 300);
        f.setLayout(null);
        f.setVisible(true);
    }
}


Run the code using the following commands:

javac Main.java
java Main

Output :

Output of AWT TextField Example

Final Output Created:

TextField and Button to take output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads