Java JTextField class

Introduction

We can use JTextField display and edit one line of plain text.

Constructors of the JTextField Class

Constructor Description
JTextField()default values for initial text, number of columns, and document.
JTextField(Document document, String text, int columns)specified document as its model
JTextField(int columns) specified columns as its number of columns.
JTextField(String text) specified text as its initial text.
JTextField(String text, int columns) specified text as its initial text and columns as its number of columns.
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class Main extends JFrame {
  public Main() {
    super("JButton");

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    // Create an empty JTextField
    JTextField emptyTextField = new JTextField();

    // Create a JTextField with an initial text of Hello
    JTextField helloTextField = new JTextField("Hello");

    // Create a JTextField with the number of columns of 20
    JTextField nameTextField = new JTextField(20);
    // w ww.  j  av a 2s.  c om

    getContentPane().add(emptyTextField);
    getContentPane().add(helloTextField);
    getContentPane().add(nameTextField);
    
  }

  public static void main(String[] args) {
    Main frame = new Main();
    frame.pack();
    frame.setVisible(true);
  }
}



PreviousNext

Related