javax.swing.JTextField
has two uses.
If you need a component that displays or allows entry of more than one line, use
a JTextArea
.
JTextField yourInpuFieldt = new JTextField(16);
content.add(yourInputField);or to add it to a
JPanel p
p.add(yourInputField);
getText()
.
yourTextField.getText()
method
whenever you need it. This is probably the most common way.
String x = yourInputField.getText();
Using a JTextField for output is almost the same as for input, but . . .
yourTextField.setText(someString)
.setEditable(false)
so the user
can't change the field.Here is the sequence.
JTextField myOutput = new JTextField(16);You can also set the initial value in the field
JTextField myOutput = new JTextField("someInitialValue", 20);
p.add(myOutput);
myOutput.setText("Some text")
.
myOutput.setText("some text");
Here are some of the most common methods use with text fields: Assume these declarations
JTextField tf; ActionListener listener; String s, text; Font f;
//--- When used for input. s = tf.getText(); // gets the text from the JTextField tf.addActionListener(listener); // Optional listener intialization. //--- When used for output. tf.setText(text); // Sets text in the JTextField. tf.setEditable(false); // Initialization to disallow user changes. //--- To change the appearance. tf.setHorizontalAlignment(align); // See below for possible values. tf.setFont(f); // sets the font for the text
yourField.setFont(Font f);
sets the font for the text.
Often numbers are aligned to the right (eg, in the display of a calculator),
and text is aligned to the left.
yourField.setHorizontalAlignment(align);
sets the alignment of the text in the field,
where align is one of these values:
JTextField.LEFT
(the default),
JTextField.CENTER
, or JTextField.RIGHT
.
JTextField userID; // declare a field . . . userID = new JTextField(8); // create field approx 8 columns wide. p.add(userID); // add it to a JPanel userID.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { ... // THIS CODE IS EXECUTED WHEN RETURN IS TYPED } } );
In addition to getting the ActionEvent when the user types an Enter in the JTextField, you should also have a button which the user can use to cause the same action because many users will not know that Enter in a JTextField will cause an event.
Use a JPasswordField
component for a field where the
characters the person enters are replaced by some other
character. JPasswordField is a subclass of JTextField.
The JPasswordField
subclass of JTextField
can be used to restrict the input to particular values.
See How to Use Formatted Text Fields.
Because JTextField is a child class of JTextComponent, you can use the methods in the TextComponent class, for example, to find and set the caret position or selected text.
Normally you would use an ActionListener to be notified when the user types the Enter key. If you need to, you can use a listener for the Document that is being displayed. These listeners can notify you about each character, selection, cursor movement, or change.
To select a JTextField so that user typing occurs in that
field, you must give the JTextField focus. For example,
the nameField
JTextField can be given focus with:
nameField.requestFocus();