TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Java Swing

    Create Text Field using JTextField

    Overview

    In this tutorial, we will learn the steps to create a text field using JTextField class.

    A text field is the GUI component that allows users to input the text in a single line. To create a text field we need to use the JTextField class.

    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.

    Create Text Field

     

    —

    Java Tutorial on this website:

    https://www.testingdocs.com/java-tutorial/

    For more information on Java, visit the official website :

    https://www.oracle.com/in/java/

    Related Posts

    Add GUI Components Swing

    Java Swing /

    Add GUI Components to a Swing Container

    JPanel Container Class

    Java Swing /

    Swing JPanel Container Class

    Swing CardLayout

    Java Swing /

    Java Swing CardLayout

    Java Swing GridLayout

    Java Swing /

    Java Swing GridLayout

    Java Swing FlowLayout

    Java Swing /

    Java Swing FlowLayout

    ‹ Setup Layout Manager to Swing Container› Create Text Area using JTextArea Class

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com