TextField
The TextField component is used for implementing or editing a single-line text entry. It performs the event on pressing and releasing a key. The TextField is subclass of the TextComponent.TextField Class Constructors
| Constructors | Description |
|---|---|
| TextField() | Constructs a new text field. |
| TextField(int columns) | Constructs a new empty text field with the specified number of columns. |
| TextField(String str) | Creates a new text field initialized with specified text. |
| TextField(String str, int columns) | Constructs a new text field with the specified text to be displayed, and it holds the specified number of columns. |
JTextComponent
Swing text components display text and optionally allow the user to edit the text. The JComponents are basically used for making style – unstyle.
JTextComponent Hierarchy
JTextComponent hierarchy is shown in below.
1. JTextField
The JTextField is a lightweight component that allows the editing of a single line of text. It is encapsulated by the JTextComponent.JTextField Constructors
| Constructors | Description |
|---|---|
| JTetxField() | Used to construct a new TextFiled. |
| JTetxField(Document doc, String str, int cols) | Constructs a new JTextField that uses the given text storage model and given number of columns. |
| JTetxField(int cols) | Constructs new empty TextField with the given number of columns. |
| JTetxField(String str) | Constructs a new TextField initialized with the specified string. |
| JTetxField(String str, int cols) | Constructs a new TextField with the specified string value and columns. |
Example: Creating textfield and buttons
// FrameTest.java
import java.awt.*;
import java.awt.event.*;
public class FrameTest extends Frame implements ActionListener
{
String title;
Button b1, b2;
TextField text1, text2;
public FrameTest(String title)
{
super(title);
setLayout(new FlowLayout());
text1 = new TextField(20);
b1 = new Button("Reset");
text2 = new TextField(20);
b2 = new Button("Submit");
add(text1);
add(b1);
add(text2);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String str = "";
if(e.getSource() == b1)
{
text1.setText(str+" You click reset button..");
}
if(e.getSource() == b2)
{
text2.setText(str+" You click submit button..");
}
}
public static void main(String args[])
{
FrameTest frame = new FrameTest("Frame with TextField & Button:");
frame.setSize(300,200);
frame.setBackground(Color.pink);
frame.setVisible(true);
}
}
Output:

2. JPasswordField
The JPassword field extends from the JTextField. It is a lightweight component that allows to type password text entry. By default, JPasswordField disable input methods, means input character could not be visible.Creating JPasswordField
JPasswordField password = new JPasswordField(10);JPasswordField Constructors
| Constructors | Description |
|---|---|
| JPasswordField() | Creates a new password field. |
| JPasswordField(Document doc, String str, int size) | Creates a new JPassword field that uses the given text storage model and specifies the password size. |
| JPasswordField(int size) | Constructs a new JPasswordField with the specified number of characters. |
| JPasswordField(String str) | Constructs the new JPassword field initialized with specified text. |
| JPasswordField(String str, int size) | Constructs a new JPasswordField initialized with the specified text and size. |
Example: Illustrating JTextField and JPasswordField
// JPasswordDemo.java
import javax.swing.*;
public class JPasswordDemo
{
public static void main(String args[])
{
JFrame frame = new JFrame("JPasswordField & JTextField");
JPanel panel = new JPanel();
JLabel l1 = new JLabel("Username");
JLabel l2 = new JLabel("Password");
JLabel l3 = new JLabel("Confirm !!!");
JTextField text = new JTextField(20);
JPasswordField pass1 = new JPasswordField(20);
JPasswordField pass2 = new JPasswordField(20);
panel.add(l1);
panel.add(text);
panel.add(l2);
panel.add(pass1);
panel.add(l3);
panel.add(pass2);
frame.add(panel);
frame.setSize(350, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Output:

3. JFormattedTextField
JFormattedTextField extends JTextField adding support for formatting arbitrary value. It provides the specific text entry component into the TextField.Creating JFormattedTextField
JFormattedTextField ftf = new JFormattedTextField();JFormattedTextField Constructor
- JFormattedTextField()
- JFormattedTextField(Format format)
- JFormattedTextField(Object value)
- JFormattedTextField(AbstractFormatter formatter)
- JFormattedTextField(AbstractFormatterFactory factory)
- JFormattedTextField(AbstractFormatterFactory factory, Objetc obj)
Example : Illustrate the JFormattedTextField
// JFormattedTextFieldDemo.Java
import javax.swing.*;
public class JFormattedTextFieldDemo
{
public static void main(String args[])
{
JFrame frame = new JFrame("JFormattedTextField ");
JPanel panel = new JPanel();
JFormattedTextField jft1 = new JFormattedTextField();
String date = new String();
date = "Date";
jft1 = new JFormattedTextField(new java.util.Date());
JLabel label1 = new JLabel(date);
panel.add(label1);
panel.add(jft1);
JFormattedTextField jft2 = new FormattedTextField(java.text.NumberFormat.getCurrencyInstance());
jft2.setValue(new Float(50.50));
JLabel label2 = new JLabel("Currency");
panel.add(label2);
panel.add(jft2);
frame.add(panel);
frame.setSize(350, 180);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Output:



