TextField
and TextArea
Class
import java.applet.Applet; import java.awt.*; public class PersonalData extends Applet { public void init() { // add name label plus text field add(new Label("Name", Label.RIGHT)); TextField name = new TextField("Surname, Initials", 25); add(name); // add address label plus text area add(new Label("Address", Label.RIGHT)); TextArea address = new TextArea(10,25); add(address); } }
The GUI components used are:
Label
TextField
TextField(String text, int numberOfCharacters)
.
In this way you get a new text field with the specified text and
wide enough to hold the specified number of characters.
TextArea
TextField
and TextArea
are
subclasses of TextComponent
and they inherit most
of their methods from this parent class such as methods for selecting
and changing text
We summarize the constructors and main methods for text areas,
text fields, and text components.
TextField
Constructors | Meaning |
TextField()
|
creates an empty text field |
TextField(int numberOfCharacters)
|
creates an empty text area with the given number of character positions (characters) |
TextField(String text)
|
creates a text field displaying the specified text |
TextField(String text,
int numberOfCharacters)
|
creates a text field with specified dimension and text |
Methods | Meaning |
getColumns()
|
returns the width of the text field |
getEchoChar()
|
returns the echo character |
setEchoChar()
|
sets the echo character |
TextComponent
.
TextArea
Constructors | Meaning |
TextArea()
|
creates an empty text area |
TextArea(int numberOfRows, int numberOfColumns)
|
creates an empty text area with the given number of rows and columns (characters) |
TextArea(String text)
|
creates a text area displaying the specified text |
TextArea(String text, int numberOfRows,
int numberOfColumns)
|
creates a text area with specified dimension and text |
Methods | Meaning |
appendText(String text)
|
appends the specified text |
getColumns()
|
returns the number of columns |
getRows()
|
returns the number of rows |
insertText(String text, int position)
|
inserts the specified text at the given character position |
replaceText(String text, int start, int end)
|
replaces the text from start to end position with the specified new text |
TextComponent
.
TextComponent
Methods | Meaning |
getSelectedText()
|
returns the selected text |
getSelectionEnd()
|
returns the selected text's end position |
getSelectionStart()
|
returns the selected text's start position |
getText()
|
returns the text of the text component |
select(start, int end)
|
selects lines from the specified start position (inclusive) to the end position (exclusive) |
selectAll()
|
selects all text in the field |
setEditable(boolean state)
|
enable (true ) or disable (false ) text to be edited
|
setText(String text)
|
sets the specified text |