AWT Components -- Text Fields & Areas
To add a text field for display or input one line of text (in this case, 30 characters wide):
- TextField tf = new TextField("initial text", 30);
- add(tf);
The text which is displayed can be changed:
- tf.setText("now show a new text");
If the user types input into the text field, it can be obtained:
- stringvar = tf.getText();
Or you can disallow the user to type:
The TextArea class also has these methods, but it can display multiple lines.
When the user types in text and presses "return" or "enter", an ActionEvent is generated, so, similarly to Buttons, an ActionListener must be provided. TextAreas generate TextEvents.