Create Text Field using JTextField
Create Text Field using JTextField
In this tutorial, we will learn how to create a text field using the JTextField class. A text field is a GUI component that allows users to input text in a single line.
Java Program
package com.testingdocs.swing.components;
/**********************************************
* FileName: JTextFieldDemo.java
* Package : com.testingdocs.swing.components
*
* Java Tutorials - www.TestingDocs.com
**********************************************/
import javax.swing.*;
public class JTextFieldDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextField - www.TestingDocs.com");
//Create Label and Text Fields
JLabel lblUName = new JLabel("Username:");
JTextField tfUName = new JTextField(30);
lblUName.setLabelFor(tfUName);
JLabel lblPass = new JLabel("Password:");
JTextField tfPass = new JTextField(30);
lblPass.setLabelFor(tfPass);
JButton btnLogin = new JButton("Login");
//Create JPanel
JPanel panel = new JPanel();
panel.add(lblUName);
panel.add(tfUName);
panel.add(lblPass);
panel.add(tfPass);
panel.add(btnLogin);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
Output
Run the program to view the output.

—
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/