Java Swing | JTextField – GeeksforGeeks

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 : 
 

  1. JTextField() : constructor that creates a new TextField
  2. JTextField(int columns) : constructor that creates a new empty TextField with specified number of columns.
  3. JTextField(String text) : constructor that creates a new empty text field initialized with the given string.
  4. JTextField(String text, int columns) : constructor that creates a new empty textField with the given string and a specified number of columns .
  5. 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: 

  1. setColumns(int n) :set the number of columns of the text field.
  2. setFont(Font f) : set the font of text displayed in text field.
  3. addActionListener(ActionListener l) : set an ActionListener to the text field.
  4. 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. 

Tóm Tắt

Java




import java.awt.event.*;

import javax.swing.*;

class text extends JFrame implements ActionListener {

    

    static JTextField t;

 

    

    static JFrame f;

 

    

    static JButton b;

 

    

    static JLabel l;

 

    

    text()

    {

    }

 

    

    public static void main(String[] args)

    {

        

        f = new JFrame("textfield");

 

        

        l = new JLabel("nothing entered");

 

        

        b = new JButton("submit");

 

        

        text te = new text();

 

        

        b.addActionListener(te);

 

        

        t = new JTextField(16);

 

        

        JPanel p = new JPanel();

 

        

        p.add(t);

        p.add(b);

        p.add(l);

 

        

        f.add(p);

 

        

        f.setSize(300, 300);

 

        f.show();

    }

 

    

    public void actionPerformed(ActionEvent e)

    {

        String s = e.getActionCommand();

        if (s.equals("submit")) {

            

            l.setText(t.getText());

 

            

            t.setText("  ");

        }

    }

}



output: 
 

 

2. Program to create a blank text field with a given initial text and given number of columns 

Java




import java.awt.event.*;

import javax.swing.*;

class text extends JFrame implements ActionListener {

    

    static JTextField t;

 

    

    static JFrame f;

 

    

    static JButton b;

 

    

    static JLabel l;

 

    

    text()

    {

    }

 

    

    public static void main(String[] args)

    {

        

        f = new JFrame("textfield");

 

        

        l = new JLabel("nothing entered");

 

        

        b = new JButton("submit");

 

        

        text te = new text();

 

        

        b.addActionListener(te);

 

        

        t = new JTextField("enter the text", 16);

 

        

        JPanel p = new JPanel();

 

        

        p.add(t);

        p.add(b);

        p.add(l);

 

        

        f.add(p);

 

        

        f.setSize(300, 300);

 

        f.show();

    }

 

    

    public void actionPerformed(ActionEvent e)

    {

        String s = e.getActionCommand();

        if (s.equals("submit")) {

            

            l.setText(t.getText());

 

            

            t.setText("  ");

        }

    }

}



output : 
 

 

3. Program to create a blank text field and set BOLD font type 

Java




import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

class text extends JFrame implements ActionListener {

    

    static JTextField t;

 

    

    static JFrame f;

 

    

    static JButton b;

 

    

    static JLabel l;

 

    

    text()

    {

    }

 

    

    public static void main(String[] args)

    {

        

        f = new JFrame("textfield");

 

        

        l = new JLabel("nothing entered");

 

        

        b = new JButton("submit");

 

        

        text te = new text();

 

        

        b.addActionListener(te);

 

        

        t = new JTextField(16);

 

        

        Font fo = new Font("Serif", Font.BOLD, 20);

 

        

        t.setFont(fo);

 

        

        JPanel p = new JPanel();

 

        

        p.add(t);

        p.add(b);

        p.add(l);

 

        

        f.add(p);

 

        

        f.setSize(300, 300);

 

        f.show();

    }

 

    

    public void actionPerformed(ActionEvent e)

    {

        String s = e.getActionCommand();

        if (s.equals("submit")) {

            

            l.setText(t.getText());

 

            

            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.

My Personal Notes

arrow_drop_up