This site requires JavaScript, please enable it in your browser!
Greenfoot back

Report as inappropriate.

dbal presents ...




I am publishing this scenario in support of an article
that I plan to publish on the topic of creating custom
GUIs using Greenfoot. If and when the article is
published, you will find a link to the article at

Note that an object of the TextBoxA class has no concept of
focus. Therefore, it must not be used concurrently
with another object that is competing for keystrokes.

This scenario demonstrates the use of an Actor class
that 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.

The getText method returns null if the user hasn't yet
stroked the Enter key. Once the user strokes the
Enter key, the string will be returned the next time
the getText method is called. At that time, the text
box is cleared and made ready to receive new input text
data from the user.

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
VERSION: 07/13/08

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
thenbe cleared, ready to accept new text input. (For an applet,
the text appears in the browser's Java Console)

11934 views / 896 in the last 7 days

3 votes | 0 in the last 7 days

Tags: demo gui textbox

This scenario is a member of: Reusable actors & support classes

open in greenfoot
Your browser does not support the canvas tag.
A new version of this scenario was uploaded on Sat Jul 19 20:39:45 UTC 2008
This is very good can you please tell me how you did this? i'm new to green foot and didn't really understand what you wrote. Thx!


I know it has been a long time but someone might considering updating this code. There are three locations where the method "getWidth" is used. That method does not exist for the Actor class. You can, however use "getImage" for the instances of the label objects in each situation and THEN use getWidth for the images. The textbox works great then.

Want to leave a comment? You must first log in.

Who likes this?

PascalFischer Rudi adamkjelstroem