I may be approaching this in the wrong way but I’m so used to Java it’s hard to think differently. I’m playing with the racket/gui to create a GUI and I want to use a choice%
, however, as far as I understand, the choices have to be strings. I want the result to be a symbol, so the choice%
box would display ‘Choice 1’ in a nice human friendly way but the selection result would be 'choice1
. This is easy in Java Swing, but how can I do it in racket without some sort of text to symbol translation function?
I don’t know Java Swing. How would it be easy there?
I’m also pretty sketchy on racket/gui but I’ve done lots of GUI in the past. Does choice%
give you the selected value as the string or as the integer offset?
My first thought would be to define a little hash-table like (define choices (hash "Choice1" 'choice1 "Choice 2" 'choice2))
. Give (hash-keys choices)
as the choices
value in (new (choice% ___))
. When I get the string selection, do (hash-ref choices the-string-choice)
to get the corresponding symbol.
And if I’m making more than a few choice%
widgets, and this starts to feel tedious, write a little helper function, or maybe derive my own symbol-choice%
from choice%
, or whatever.
@mark.warren But I don’t know if that’s great advice; maybe someone with more racket/gui experience can chime in.
Oh carp, I got the hash-table keys and values backwards there.
Edited that to fix.
@greg Thanks for that, I’ve just realised it gives you the integer so you can use that as a lookup.
With Swing you just add a load of objects to the choice box and when you get the selected item you get that object back (The displayed string in the box is the toString() method of the object.
The racket/gui stuff is a bit lower level so it’s probably better to derive a new choice% class that has what I need. @greg thanks for the pointers.
My guess is that racket/gui uses OOP/classes because that’s a good fit for the OS GUI frameworks, apples and apples. But my other guess is that it doesn’t presume all your other Racket code will be OO, so, it sticks to primitives — like list of strings here.
@greg I think you are probably right. I’m gonna have to dive a bit more into racket objects.