JTextField is a component in Java Swing that allows for single-line text input.javax.swing package and extends JTextComponent, providing a user interface for entering and editing text.
| Constructor | Description |
|---|---|
JTextField() |
Creates a new JTextField with a default width of 0 columns. |
JTextField(int columns) |
Creates a new JTextField with the specified number of columns. |
JTextField(String text) |
Creates a new JTextField initialized with the specified text. |
JTextField(String text, int columns) |
Creates a new JTextField initialized with the specified text and columns. |
package jtextfields;
import javax.swing.*;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import java.awt.*;
public class JTextFieldExample extends JFrame {
JTextField textField1,textField2,textField3,textField4;
public JTextFieldExample() {
// Create a JFrame to hold everything
setTitle("JTextField Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLayout(null);
// Create JTextField using default constructor
textField1 = new JTextField();
textField1.setText("Default Constructor");
textField1.setBounds(50, 20, 120, 30);
add(textField1);
// Create JTextField with specified columns
textField2 = new JTextField(10);
textField2.setText("10 Columns");
textField2.setBounds(50, 60, 120, 30);
add(textField2);
// Create JTextField with specified text
textField3 = new JTextField("Specified Text");
textField3.setBounds(50, 100, 120, 30);
add(textField3);
// Create JTextField with specified text and columns
textField4 = new JTextField("Text and Columns", 15);
textField4.setBounds(50, 140, 120, 30);
add(textField4);
// Manipulate JTextField methods
textField1.setColumns(20);
textField2.setHorizontalAlignment(JTextField.CENTER);
textField3.setEditable(false);
textField4.setMargin(new Insets(5, 5, 5, 5));
// Display the frame
setVisible(true);
}
public static void main(String[] args) {
new JTextFieldExample();
}
}

| Method | Description |
|---|---|
String getText() |
Returns the text contained in this TextComponent. |
void setText(String t) |
Sets the text of this TextComponent to the specified text. |
int getColumns() |
Returns the number of columns in this TextComponent. |
void setColumns(int columns) |
Sets the number of columns in this TextComponent. |
int getHorizontalAlignment() |
Returns the horizontal alignment of the text. |
void setHorizontalAlignment(int alignment) |
Sets the horizontal alignment of the text. Possible values are JTextField.LEFT, JTextField.CENTER, JTextField.RIGHT, JTextField.LEADING, and JTextField.TRAILING. |
void setEditable(boolean b) |
Sets the specified boolean to determine whether the TextComponent is editable. |
boolean isEditable() |
Returns true if the TextComponent is editable, otherwise false. |
void setDocument(Document doc) |
Associates the editor with a text document. |
Document getDocument() |
Returns the Document model associated with this TextComponent. |
void setMargin(Insets m) |
Sets the margin between the text field’s border and its text. |
Insets getMargin() |
Returns the margin between the text field’s border and its text. |
You must be logged in to submit a review.