90 CHAPTER 7. GRAPHICAL USER INTERFACE DESIGN IN SCHEME
7.10
Textfields
A textfield provides a place for user's to write text. The following demo shows
how to write a doubler program that multiplies whatever the user writes by
2. The action is taken when the user hits the enter key:
(define w
(window "Hello"
(textfield "1" 20
(action (lambda(e)
(let* ((t (.getSource e))
(x (readexpr t))
(y (* 2 x)))
(writeexpr t y)))))))
(.pack w)
(.show w)
The first two arguments to textfield are the initial contents (in this case the
number 1) and the size of the textfield (in columns). One can also set the font
and background color of the textfield.
7.11
Textareas
A textarea is like a textfield except that it has multiple lines and textareas
cannot accept actions. They are passive objects. You can write on them and
read from them, but they don't cause actions to occur. The following demo
shows how to have the user's choice of a language write the appropriate greeting
in the textarea:
(define ta (textarea 10 50)); 10 rows and 50 cols
(define w
(window "Hello"
(col
(choice "English" "Spanish" "Japanese"
(action (lambda(e)
(case (readstring (.getSource e))
(("English") (writeexpr ta "Hello"))
(("Spanish") (writeexpr ta "Hola"))
(("Japanese") (writeexpr ta "Konichi wa"))
))))
ta)))
(.pack w)
(.show w)
Note also that we gave the textarea a name (ta) so that we could refer to it in
the writeexpr expression. We also used the name to include it in a column in
the window.