Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
TextField class
2.1.
AWT TextField Class Declaration
2.2.
TextField Class Constructors
3.
TextField Class Methods
4.
Examples
5.
Java AWT TextField Example with ActionListener
6.
Frequently Asked Questions
6.1.
What is the TextField in Java?
6.2.
What is the difference between TextField and password field?
6.3.
Why AWT is heavyweight?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

AWT TextField

Author Sanjana Yadav
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

A TextField object is a text component that allows a user to enter and change a single line of text. It inherits the TextComponent class, which in turn inherits the Component class.

When we insert a key in the text field (such as key pushed, key released, or key typed), the event is transmitted to TextField. The KeyEvent is then sent to the registered KeyListener. It is also possible to use ActionEvent; if the ActionEvent is enabled on the text field, the ActionEvent may be triggered by pressing the return key. The ActionListener interface is in charge of handling the event.

Let us now understand the AWT TextField in detail, with the help of examples.

TextField class

A GUI TextField is created using the TextField class. It shows a text line that may be edited. This indicates that the user can modify the content shown by TextField. This class extends the TextComponent class, which derives from the Component class.

The AWT generates events whenever a key is pushed in a TextField. A key pressed event, a key released event, or a key typed event might all occur. The registered KeyListener receives a KeyEvent. When the 'enter' key is pushed, a TextField can additionally emit an ActionEvent. Any class that wishes to participate in this event must implement the ActionListener interface.

AWT TextField Class Declaration

public class TextField extends TextComponent 

TextField Class Constructors

The table below lists several types of TextField class constructors.

You can also read about the Multiple Inheritance in Java.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

TextField Class Methods

The AWT TextFeild inherits the methods listed above from the below classes

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

Let us now practice some examples of the AWT TextField, in order to get a better understanding of the topic.

Examples

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TextFieldExample {
    Button b;
    Label l1, l2, l3, l4, l5;
    TextField f1, f2, f3, f4, f5;
    String str = "";
    Frame jf;

    TextFieldExample() {
        jf = new JFrame("Hello");

        l1 = new Label("Textfield-1");
        l2 = new Label("Textfield-2");
        l3 = new Label("TextField-3");
        l4 = new Label("TextField-4");
        l5 = new Label("TextField-5");

        f1 = new TextField(); // calling TextField() constructor
        f2 = new TextField("Welcome to Codiing Ninjas!"); // calling TextField(String) Constructor
        f3 = new TextField(5); // calling TextField(int) constructor
        f4 = new TextField("Happy Coding Ninjas", 15); // calling TextField(String, int) constructor

        // Setting the font color to blue, in a TextField
        f2.setForeground(Color.BLUE);

        f4.setEditable(false); // Making a text field uneditable
        f4.setFont(new Font("Serif", Font.BOLD, 10)); // Setting a font style in a TextField

        jf.setLayout(new FlowLayout());
        jf.add(l1);
        jf.add(f1);
        jf.add(l2);
        jf.add(f2);
        jf.add(l3);
        jf.add(f3);
        jf.add(l4);
        jf.add(f4);

        jf.setSize(450, 150);
        jf.setVisible(true);
    }

    public static void main(String... ar) {
        new TextFieldExample();
    }
}

Output

When you execute the above code, you'll see the following window:

Practice it on online java compiler for better understanding.

Java AWT TextField Example with ActionListener

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TextFieldExample implements ActionListener
{
Button b;
Label l1, l2, l3, l4;
TextField f1, f2, f3;
Frame jf;
TextFieldExample()
{
jf = new JFrame("Handling TextField Event");
jf.setSize(340,260);


l1= new Label("Enter your class");
l2= new Label("Enter your subject");
l3= new Label("Enter your marks");



f1 = new TextField(20);
f2 = new TextField(20); //calling TextField(String)
f3 = new TextField(20);


b = new Button("VIEW");


jf.setLayout(new FlowLayout());


jf.add(l1);
jf.add(f1);
jf.add(l2);
jf.add(f2);
jf.add(l3);
jf.add(f3);
jf.add(b);



b.addActionListener(this); //As soon as the button is clicked, data from all the textfields is read
jf.setVisible(true);
}


public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("VIEW"))
{
l4= new Label("", Label.CENTER);
l4.setText("Your Class : " + f1.getText()+ ", Subject : " + f2.getText()+ ", Your Marks :" + f3.getText());
jf.add(l4);
jf.setVisible(true);
}
}
public static void main(String... ar)
{
new TextFieldExample();
}
}

Output

When you execute the above code, you'll see the following window:

You may now fill in the data in each textfield.

When you hit the VIEW button, the data you entered is shown.

Frequently Asked Questions

What is the TextField in Java?

A TextField object is a text component that allows a single line of text to be edited.

What is the difference between TextField and password field?

The acquired text is displayed in an unencrypted form in the TextField, whereas the obtained text is displayed in an encrypted form in the password field. This component permits single-line sensitive input, such as passwords.

Why AWT is heavyweight?

Because its components are dependent on the underlying Operating System, AWT is considered heavy-weight. When we construct an object of the java. awt. Checkbox class, for example, the underlying Operating System creates a checkbox for us.

Conclusion

In this article, we have extensively discussed the Java AWT TextField. We learned about the properties, constructors, methods of a TextField, and how to create them.

We hope that this blog has helped you enhance your knowledge of java and java awt and if you would like to learn more about java, check out our articles, java archivesDifference Between AWT And Swing In Java | Coding Ninjas Blog

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Previous article
Java AWT Label
Next article
AWT TextArea
Live masterclass