Introduction | Your first interface | Building a model | Advanced features | Resources

4.4 Inputting and displaying text

This section demonstrates the different visualisation objects for displaying and inputting text. The model and interfaces used in this section can be downloaded here: trainingText.zip. The objects are (interface in brackets):

  • Text string (trainingTextString.jsm)
  • Model text area (trainingTextArea.jsm)
  • User input text area (trainingTextInputArea.jsm)
  • Text menu (trainingTextMenu.jsm)

Text string

A text string is used for displaying or inputting single words or lines of text. In the example below several strings are used to display text on the left, and one is used to input it on the right.

The modelProperty property sets the text to be displayed to a string property. The acceptInput property allows you to control whether the object can be used to accept user input. You can also limit the number of characters a user can input with the maxLength property.

Superscripts and subscripts can be displayed in a text string if the checkScripts property is set to true. Superscripts must be enclosed in SUP tags and subscripts in SUB tags. The main font size and script font size can be set separately.

Model text area

The model text area is used for displaying longer strings of text. The area reformats if it is resized. The example below shows the same string formatted in three different ways.

The modelProperty property sets the string to be displayed, or you can set this manually using the text property if a model property is not assigned. Text can be justified left, right, or centrally with the justification property.

User input text area

The user input text area is used to accept larger strings of textual input from the user. In example below, an input text area is shown on the left with a model text area on the right. See how the model text area updates when you type in the input text area.

The modelProperty property sets the text string to be displayed.

User input text areas are heavyweight objects and when using them in the interface builder you may find that all other objects tend to disappear behind. This is annoying but will not affect the final simulation. They will be updated to stop this happening in a future release.

Text menu

The text menu is used to provide a list from which a user can select an item, and to display that choice. In the example below you can click on the animal menu to display a range of possible choices.

Menus must be defined in the code with a 1-dimensional array of stings and an integer variable that defines the selected menu value. For instance:

	public String animalMenu[] = {"cat","dog","fish","mouse","horse"};
	public int animalSelected = 0;

The naming convention is important here - the word before 'Menu[]' and 'Selected' must be the same, in this case 'animal'. The menu must also be registered in code:

registerMenuProperty("Animal Menu", "animal", "updateAnimalSelected");

Note that the second parameter must be the same as the word used earlier.

You must link the modelProperty property to the string array.