Text Fields and Text Areas

 

Text Fields

A text field is an input/output region on the screen that holds one line of string data.  A text field is useful for entering or displaying such things as a person’s name.  Text fields are declared in much the same manner as other window controls:

 

TextField <name of field> =
          addTextField ("<initial string>",

                         <row>, <col>,
                         <width>, <height>);  

 

The next table shows three commonly used text field methods:

 

Name of Method

What the Method Does

SetText (aString)

  returns void

Display aString on the screen.  Nothing is returned.

getText()

  returns String

Return the string entered on the screen.

setEditable (true/false)

  returns void

Enables or disables editing of the field.

 

Text fields usually open with no initial display of data.  For example, the following code displays a label and an empty text field for entering a person's name:

 

Label nameLabel = addLabel ("Name",1,1,1,1);

TextField nameField = addTextField ("",1,2,1,1);

 

The method setEditable can also be used with IntegerField and DoubleField.  This method is useful for making a field read only.  For example,

 

nameField.setEditable (false);  // Read-only field

 

Text Areas

A text area is similar to a text field, except that it can handle several lines of text at a time.  A text area can be used for entering or displaying an address, table, schedule, or any multiline descriptive information.  Text areas are declared as follows:

 

TextArea <name of field> =
      addTextArea ("<initial string>",

                    <row>, <col>, <width>, <height>);  

 

Whereas a text field typically has a width and height of one, a text area is usually several columns wide and high.  Here are some methods that manipulate text areas:

 

Name of Method

What the Method Does

setText (aString)

  returns void

Display aString on the screen.  Nothing is returned.

append (aString)

  returns void

Append aString to the text already displayed on the screen.

getText()

  returns aString

Return the string entered on the screen.

 

The following code would create a text area that allows the display of 5 rows of text:

 

TextArea output = addTextArea ("", 1, 1, 2, 5);

 

Text areas have scroll bars, similar to those used with text editors, to allow the user to view hidden parts of the output data.

 

 

martin@cc.wwu.edu
Disclaimer

Copyright Martin Osborne 1998-2001
  All rights reserved