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();
}
}

You can also try this code with Online Java Compiler
Run Code
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();
}
}

You can also try this code with Online Java Compiler
Run Code
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 archives, Difference 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 Algorithms, Competitive Programming, JavaScript, System 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 problems, interview 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!