TextField

TextField objects are used for entering text on a user interface.

The following functions creates a TextField object, so you need to store it in a variable (or other memory location) to be able to use it later.

Function Description
TextField(text, columns, function) Creates a text field containing specified text (a string – optional), with specified number of columns width (optional – default is 8), with function to call when the ENTER key is pressed in the text field.  This function should expect one parameter (string – the contents of the text field).

If you create a TextField with a callback function, then that function will be called anytime the enter key is typed inside the box.  (Presumably, the user will change the text and then press enter.)

For example, a text field may be created as follows:

text = TextField("type and hit  ", 18, processEntry)

where processEntry is a function which expects one parameter, the updated text (a string).

Once a TextField has been created, it may be added to a Display specifying where to place its top-left corner.

d.add(textfield1, 50, 50)

If you create a TextField without using a callback function, then it is a passive GUI element.  In other words, a different part of your program needs to manage (e.g., check for change in) the text content.  This can be done using the following functions:

Function Description
textField.getText() Returns the text contained in the text field (as a string).
textField.setText() Sets the text contained in the text field (as a string).
textField.setFont() Changes the font used in the text field,e.g., Font(“Dialog”, Font.PLAIN, 12), or Font(“Serif”, Font.ITALIC, 16).