mark.warren
2018-11-30 14:34:00

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?


greg
2018-11-30 14:45:13

I don’t know Java Swing. How would it be easy there?


greg
2018-11-30 14:46:17

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?


greg
2018-11-30 14:52:49

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.


greg
2018-11-30 14:53:22

@mark.warren But I don’t know if that’s great advice; maybe someone with more racket/gui experience can chime in.


greg
2018-11-30 14:54:15

Oh carp, I got the hash-table keys and values backwards there.


greg
2018-11-30 14:55:50

Edited that to fix.


mark.warren
2018-11-30 14:56:25

@greg Thanks for that, I’ve just realised it gives you the integer so you can use that as a lookup.


mark.warren
2018-11-30 14:57:56

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.


mark.warren
2018-11-30 15:00:52

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.


greg
2018-11-30 15:05:05

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.


mark.warren
2018-11-30 15:11:59

@greg I think you are probably right. I’m gonna have to dive a bit more into racket objects.