Text Area
The TextField and TextArea classes are two different Java classes for entering text data. The TextField class handles only one line of text. The TextArea class handles multiple lines. The example on this page illustrates text fields and a text echo. The following line will create a TextField with 20 columns.
TextField plaintext = new TextField(20);
The text field can also be initialized with some text when it is created.
TextField plaintext = new TextField("First Name Last Name");
The following methods may be used with text fields and text areas.
plaintext.setText("Hello");
.
String mydata = plaintext.getText();
String thedata = plaintext.getSelectedText();
.
int first, last;
first = plaintext.getSelectionStart();
last = plaintext.getSelectionEnd();
plaintext.selectAll(); plaintext.setEchoCharacter('*');
.
.
char echosymbol =plaintext.getEchoChar();
.
int number = plaintext.getColumns();
|
Put
text into the text field or text area. Get the text that is in the text field. Get the text that has been selected by the mouse. Get the integers designating the first and last positions of the text. Select the entire text. For text fields only, set up an echo character which is printed instead of the text entered (such as those used when passwords are entered.) Find out the echo
character. |
The TextField class calls the action method when the user presses return. The TextArea class does not use the action method. The following example demonstrates the use of text fields
The code for the Applet is listed below
import java.applet.*;
import java.awt.*;
public class textfld extends Applet
{
TextField plaintext = new TextField(20);
TextField hiddentext = new TextField(10);
Label plaintextvalue = new Label
("----------------------------------");
Label hiddentextvalue = new Label
("-----------------------------------");
|
Import all the facilities of
the AWT and applet that Java has to offer. Create an applet textfld. Create two text fields, with 20 and 10 columns, respectively. Create two labels containing lines. |
public void init()
{
add(new Label("Type text in either text
box and hit the enter key."));
add(plaintext);
hiddentext.setEchoCharacter('*');
add(hiddentext);
add(new Label("Or hit this button to"));
add(new Button("Enter both fields"));
add(new Label("at once."));
add(new Label("In the plain box you
typed "));
add(plaintextvalue);
add(new Label("In the hidden box you
typed "));
add(hiddentextvalue);
}
|
Add 5 labels and 2 text fields. The second text field, hiddentext, has '*' as an echo character. The plaintextvalue is added to one label when the event is handled. The hiddenttextvalue is added to the other label when the event is handled. |
public boolean action(Event evt,
Object whatAction)
{
if (evt.target == plaintext)
{
plaintextvalue.setText
(plaintext.getText());
return true;
}
if (evt.target == hiddentext)
{
hiddentextvalue.setText
(hiddentext.getText());
return true;
}
if((evt.target instanceof Button))
{
String buttonLabel = (String)
whatAction;
if (buttonLabel == "Enter both
fields")
{
hiddentextvalue.setText
(hiddentext.getText());
plaintextvalue.setText
(plaintext.getText());
return true;
}
}
return false;
}
}
|
When a button is pressed or
the user presses the enter key from within a text box an
action event is triggered. The evt.target attribute will
contain the type of action. If the action took place in
the plain text box we get the text from plain text box
and copy it to the plain text label and return true to
indicate the event was handled. We do the same for the hidden text box. Finally check to see if the button was pressed. If it is the button we want Set the label for both the hidden and plain text values. and return true to signal that the action was handled. Return false if the text boxes or the button did not trigger this action. |
The easiest way to obtain the code for this Applet is to click here and copy the contents of the screen that loads in your browser , paste it in a text editor and save it in a file called "textfld.java".
After compiling this program we need to create an HTML page with a reference to the class file. It will require a graphic window that is large enough to contain the two lists, buttons, and label (width-301 and height-301).
Save the HTML code in the same directory as the class file.
When you load the HTML file in your browser the applet will execute as below.
Back to the Java AWT Components Page