AWT TextField Class

The textField component allows the user to edit single line of text.When the user types a key in the text field  the event is sent to the TextField. The key event may be key pressed, Key released or key typed. The key event is passed to the registered KeyListener. It is also possible to for an ActionEvent if the ActionEvent is enabled on the textfield then ActionEvent may be fired by pressing the return key.
Following is the declaration for java.awt.TextField class:
public class TextField
   extends TextComponent
TextField() 
 Constructs a new text field.
TextField(int columns)
Constructs a new empty text field with the specified number of columns.
TextField(String text)  
Constructs a new text field initialized with the specified text.
TextField(String text, int columns)  
Constructs a new text field initialized with the specified text to be displayed, and wide enough to hold the specified number of columns.
void addActionListener(ActionListener l) 
Adds the specified action listener to receive action events from this text field.
void addNotify() 
Creates the TextField's peer.
boolean echoCharIsSet() 
Indicates whether or not this text field has a character set for echoing.
AccessibleContext getAccessibleContext() 
Gets the AccessibleContext associated with this TextField.
ActionListener[] getActionListeners() 
Returns an array of all the action listeners registered on this textfield.
int getColumns() 
Gets the number of columns in this text field.
char getEchoChar() 
Gets the character that is to be used for echoing.
<T extends EventListener> T[] getListeners(Class<T> listenerType) 
Returns an array of all the objects currently registered as FooListeners upon this TextField.
Dimension getMinimumSize() 
Gets the minumum dimensions for this text field.
Dimension getMinimumSize(int columns) Gets the minumum dimensions for a text field with the specified number of columns.
Dimension getPreferredSize() 
Gets the preferred size of this text field.
Dimension getPreferredSize(int columns) 
Gets the preferred size of this text field with the specified number of columns.
Dimension minimumSize() 
 Deprecated. As of JDK version 1.1, replaced by getMinimumSize().
Dimension minimumSize(int columns) 
Deprecated. As of JDK version 1.1, replaced by getMinimumSize(int).
protected String paramString() 
Returns a string representing the state of this TextField.
Dimension preferredSize() 
Deprecated. As of JDK version 1.1, replaced by getPreferredSize().
Dimension preferredSize(int columns) 
Deprecated. As of JDK version 1.1, replaced by getPreferredSize(int).
protected void processActionEvent(ActionEvent e) 
Processes action events occurring on this text field by dispatching them to any registered ActionListener objects.
protected void processEvent(AWTEvent e) 
Processes events on this text field.
void removeActionListener(ActionListener l) 
Removes the specified action listener so that it no longer receives action events from this text field.
void setColumns(int columns) 
Sets the number of columns in this text field.
void setEchoChar(char c) 

Sets the echo character for this text field.
void setEchoCharacter(char c) 
Deprecated. As of JDK version 1.1, replaced by setEchoChar(char).
void setText(String t) 
Sets the text that is presented by this text component to be the specified text.
This class inherits methods from the following classes:
java.awt.TextComponent
java.awt.TextComponent
java.awt.Component
java.awt.Component
java.lang.Object
java.lang.Object
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showTextFieldDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showTextFieldDemo(){
      headerLabel.setText("Control in action: TextField"); 

      Label  namelabel= new Label("User ID: ", Label.RIGHT);
      Label  passwordLabel = new Label("Password: ", Label.CENTER);
      final TextField userText = new TextField(6);
      final TextField passwordText = new TextField(6);
      passwordText.setEchoChar('*');

      Button loginButton = new Button("Login");
   
      loginButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {     
            String data = "Username: " + userText.getText();
            data += ", Password: " + passwordText.getText();
            statusLabel.setText(data);        
         }
      }); 

      controlPanel.add(namelabel);
      controlPanel.add(userText);
      controlPanel.add(passwordLabel);       
      controlPanel.add(passwordText);
      controlPanel.add(loginButton);
      mainFrame.setVisible(true);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT  and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output

    13 Lectures 
     2 hours 

 EduOLC
 Print
 Add Notes
 Bookmark this page