Report as inappropriate.
This is a modest update to the earlier TextBox class
named TextBoxA published at
Any object that instantiates an object of this class
named TextBoxB must implement the TextBoxListener
interface and define the callback method. If you don't
want to do that, use the earlier version of the class
Note that an object of this class has no concept of
focus. Therefore, it must not be used concurrently
with another object that is competing for keystrokes.
The Actor class named TextBoxB can be used to instantiate
a text box that will accept text input from the user and
return that text as a String object when the getText
method is called on the object. This updated version
named TextBoxB adds the capability to register the user
object as a listener and to notify the listener when text
is ready for retrieval, thus eliminating the polling
requirement of the earlier version named TextBoxA. This
version can be used either way by either:
1 - Calling the getText method in the act method and
leaving the callback method empty.
2 - Calling the getText method from within the callback
method and not calling the getText method in the act
The getText method returns null if the user hasn't yet
stroked the Enter key. When the user strokes the
Enter key, the callback method is called to notify the
user object that text is ready to be retrieved.
The string contents of the text box will be returned
the next time the getText method is called. When the
getText method is called, the text box is cleared and
made ready to receive new text data.
The user object should either poll the text box by
calling the getText method from inside the act method, or
should call the getText method when its callback method
is called, but not both.
The text box supports the use of the backspace key to
delete text that has been entered one character at a
time from the right end of the text string. It does not
support arrow keys or functions keys. If those keys are
stroked, the resulting string will contain the names
of those keys.
Author Dick Baldwin
HOW TO START THIS PROJECT: Click the Run button to start.
USER INSTRUCTIONS: Type text into the text box and stroke
the Enter key when you reach the end of the text. The text
that you entered should appear on the Greenfoot Terminal
Window when you stroke the Enter key. The text box should
then be cleared, ready to accept new text input. (For an
applet, the text appears in the browser's Java Console)
8502 views / 11 in the last 7 days
6 votes | 0 in the last 7 days
This scenario is a member of: Reusable actors & support classes
Want to leave a comment? You must first log in.