Login    New User    Forgot Password    Help      

How to Use Text Fields

Java Tutorials

A text field is a basic text control that enables the user to type a small amount of text. When the user indicates that text entry is complete (usually by pressing Enter), the text field fires an action event. If you need to obtain more than one line of input from the user, use a text area.

The Swing API provides several classes for components that are either varieties of text fields or that include text fields.

JTextField What this section covers: basic text fields.
JFormattedTextField A JTextField subclass that allows you to specify the legal set of characters that the user can enter. See How to Use Formatted Text Fields.
JPasswordField A JTextField subclass that does not show the characters that the user types.
JComboBox Can be edited, and provides a menu of strings to choose from.
JSpinner Combines a formatted text field with a couple of small buttons that enables the user to choose the previous or next available value.


Example

import java.awt.*;
import javax.swing.*;

public class swing2 extends JApplet
{

JTextField jtf;

public void init()
{

Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());

jtf = new JTextField(15);
contentPane.add(jtf);
}
}


Output

Output in applet

Java Tutorials