Java Swing | JTextField - GeeksforGeeks

03 Dec, 2021
JTextField is a part of javax.swing package. The class JTextField is a component that allows editing of a single line of text. JTextField inherits the JTextComponent class and uses the interface SwingConstants.The constructor of the class are :  
JTextField() : constructor that creates a new TextFieldJTextField(int columns) : constructor that creates a new empty TextField with specified number of columns.JTextField(String text) : constructor that creates a new empty text field initialized with the given string.JTextField(String text, int columns) : constructor that creates a new empty textField with the given string and a specified number of columns .JTextField(Document doc, String text, int columns) : constructor that creates a textfield that uses the given text storage model and the given number of columns.
JTextField() : constructor that creates a new TextField
JTextField(int columns) : constructor that creates a new empty TextField with specified number of columns.
JTextField(String text) : constructor that creates a new empty text field initialized with the given string.
JTextField(String text, int columns) : constructor that creates a new empty textField with the given string and a specified number of columns .
JTextField(Document doc, String text, int columns) : constructor that creates a textfield that uses the given text storage model and the given number of columns.
Methods of the JTextField are: 
setColumns(int n) :set the number of columns of the text field.setFont(Font f) : set the font of text displayed in text field.addActionListener(ActionListener l) : set an ActionListener to the text field.int getColumns() :get the number of columns in the textfield.
setColumns(int n) :set the number of columns of the text field.
setFont(Font f) : set the font of text displayed in text field.
addActionListener(ActionListener l) : set an ActionListener to the text field.
int getColumns() :get the number of columns in the textfield.
Following are the programs to implement JTextField. 1. Program to create a blank text field of definite number of columns. 
Java
// Java program to create a blank text// field of definite number of columns.import java.awt.event.*;import javax.swing.*;class text extends JFrame implements ActionListener {    // JTextField    static JTextField t;     // JFrame    static JFrame f;     // JButton    static JButton b;     // label to display text    static JLabel l;     // default constructor    text()    {    }     // main class    public static void main(String[] args)    {        // create a new frame to store text field and button        f = new JFrame("textfield");         // create a label to display text        l = new JLabel("nothing entered");         // create a new button        b = new JButton("submit");         // create a object of the text class        text te = new text();         // addActionListener to button        b.addActionListener(te);         // create a object of JTextField with 16 columns        t = new JTextField(16);         // create a panel to add buttons and textfield        JPanel p = new JPanel();         // add buttons and textfield to panel        p.add(t);        p.add(b);        p.add(l);         // add panel to frame        f.add(p);         // set the size of frame        f.setSize(300, 300);         f.show();    }     // if the button is pressed    public void actionPerformed(ActionEvent e)    {        String s = e.getActionCommand();        if (s.equals("submit")) {            // set the text of the label to the text of the field            l.setText(t.getText());             // set the text of field to blank            t.setText("  ");        }    }}
output:  
 
2. Program to create a blank text field with a given initial text and given number of columns 
Java
// Java program to create a blank text field with a// given initial text and given number of columnsimport java.awt.event.*;import javax.swing.*;class text extends JFrame implements ActionListener {    // JTextField    static JTextField t;     // JFrame    static JFrame f;     // JButton    static JButton b;     // label to display text    static JLabel l;     // default constructor    text()    {    }     // main class    public static void main(String[] args)    {        // create a new frame to store text field and button        f = new JFrame("textfield");         // create a label to display text        l = new JLabel("nothing entered");         // create a new button        b = new JButton("submit");         // create a object of the text class        text te = new text();         // addActionListener to button        b.addActionListener(te);         // create a object of JTextField with 16 columns and a given initial text        t = new JTextField("enter the text", 16);         // create a panel to add buttons and textfield        JPanel p = new JPanel();         // add buttons and textfield to panel        p.add(t);        p.add(b);        p.add(l);         // add panel to frame        f.add(p);         // set the size of frame        f.setSize(300, 300);         f.show();    }     // if the button is pressed    public void actionPerformed(ActionEvent e)    {        String s = e.getActionCommand();        if (s.equals("submit")) {            // set the text of the label to the text of the field            l.setText(t.getText());             // set the text of field to blank            t.setText("  ");        }    }}
output :  
 
3. Program to create a blank text field and set BOLD font type 
Java
// Java program to create a blank text field and set BOLD font typeimport java.awt.event.*;import java.awt.*;import javax.swing.*;class text extends JFrame implements ActionListener {    // JTextField    static JTextField t;     // JFrame    static JFrame f;     // JButton    static JButton b;     // label to display text    static JLabel l;     // default constructor    text()    {    }     // main class    public static void main(String[] args)    {        // create a new frame to store text field and button        f = new JFrame("textfield");         // create a label to display text        l = new JLabel("nothing entered");         // create a new button        b = new JButton("submit");         // create a object of the text class        text te = new text();         // addActionListener to button        b.addActionListener(te);         // create a object of JTextField with 16 columns        t = new JTextField(16);         // create an object of font type        Font fo = new Font("Serif", Font.BOLD, 20);         // set the font of the textfield        t.setFont(fo);         // create a panel to add buttons and textfield        JPanel p = new JPanel();         // add buttons and textfield to panel        p.add(t);        p.add(b);        p.add(l);         // add panel to frame        f.add(p);         // set the size of frame        f.setSize(300, 300);         f.show();    }     // if the button is pressed    public void actionPerformed(ActionEvent e)    {        String s = e.getActionCommand();        if (s.equals("submit")) {            // set the text of the label to the text of the field            l.setText(t.getText());             // set the text of field to blank            t.setText("  ");        }    }}
output :  
 
 
Note : The above programs might not run in an online compiler use an offline IDE. The initial text, the font and the number of columns of the textfield are arbitrary and can be changed by the programmer as per their need.
nidhi_biet
Akanksha_Rai
sweetyty
clintra
java-swing
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
How to iterate any Map in Java
Initialize an ArrayList in Java
Interfaces in Java
ArrayList in Java
Multidimensional Arrays in Java
Stack Class in Java
Singleton Class in Java
LinkedList in Java