Friday, December 27, 2024
Google search engine
HomeLanguagesJavaJava Swing | JTextField

Java Swing | JTextField

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. 

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 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 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 type
import 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.

RELATED ARTICLES

Most Popular

Recent Comments