Java Swing JTextField
JTextField is the simplest Swing text component. It is also probably its most widely used text component. JTextField allows you to edit one line of text. It is derived from JTextComponent, which provides the basic functionality common to Swing text components. JTextField uses the Document interface for its model.
JTextField(int cols) JTextField(String str, int cols) JTextField(String str)
Here, str is the string to be initially presented, and cols is the number of columns in the text field. If no string is specified, the text field is initially empty. If the number of columns is not specified, the text field is sized to fit the specified string.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TextField extends JApplet { JTextField jtf; public void init() { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { makeGUI(); } }); } catch(Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { setLayout(new FlowLayout()); jtf = new JTextField(15); add(jtf); jtf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { showStatus(jtf.getText()); } }); } }