248
The Java Abstract Windowing Toolkit
M
T
W
R
F
S
S
DAY
DAY
13
P2/V4sqc 7 TY Java in 21 Days 030-4 sdv 12.22.95
Ch 13 LP#3
You can also create a text field that obscures the characters typed into it--for example, for
password fields. To do this, first create the text fields itself, and then use the
setEchoCharacter()
method to set the character that is echoed on the screen. Here is an example:
TextField tf = new TextField(30);
tf.setEchoCharacter('*');
Figure 13.8 shows three text boxes (and labels) that were created by using the following code:
add(new Label("Enter your Name"));
add(new TextField("your name here",45));
add(new Label("Enter your phone number"));
add(new TextField(12));
add(new Label("Enter your password"));
TextField t = new TextField(20);
t.setEchoCharacter(`*');
add(t);
Figure 13.8.
Text fields.
Text fields inherit from the class
TextComponent
and have a whole suite of methods, both
inherited from that class and defined in its own class, that may be useful to you in your Java
programs. Table 13.4 shows a selection of those methods.
Table 13.4. Text field methods.
Method
Action
getText()
Returns the text this text field contains (as a string)
setText(String)
Puts the given text string into the field
getColumns()
Returns the width of this text field
select(int, int)
Selects the text between the two integer positions (positions
start from
0
)
selectAll()
Selects all the text in the field
030-4s CH13.i
1/29/96, 10:34 PM
248