Last week I quipped to a colleague “Why wouldn’t one take advantage of both Groovy and JavaFX within a project!” Groovy is a clean and modern dynamic scripting language. It is indeed more terse than Java, but sadly no prettier than Swing for doing a GUI. But now there is eye candy capable JavaFX. So, could this be a marriage made in heaven? A little code might shed some light.
If you read my words on wishing to “have my cake and eat it“, I should follow that up by creating an alphabetical list in Groovy, and then making use of that list within JavaFX.
OK, in Groovy, it’s so easy.
def letters=('a'..'z')
So as to make it something that JavaFX can call, I’ll wrap that line up in a simple class with a single method.
$ cat lettersG.groovy
public class lettersG {
public String[] getLetters() {
return ( ('a'..'z') )
}
static void main(args) {// TEST
for (letter in new lettersG().getLetters())
print "<${letter}>" // yes with markers < > so that we are sure we have an array of individual items
}
}
Couldn’t be simpler (and certainly a lot shorter than Java). A quick compile and test shows us what we have created.
$ groovyc lettersG.groovy $ groovy lettersG
Output: <a><b><c><d><e><f><g><h><i><j><k><l><m><n><o><p><q><r><s><t><u><v><w><x><y><z>
I was also curious as to what would get exported from the class. It’s a class, so ‘javap’ can help with that.
$ javap -public lettersG | grep -i getLetters
public java.lang.String[] getLetters();
Yes, we’re in business, this would cause one no problems (very unlike C trying to call a name mangled C++ method. Those of us who upgraded from C to C++ soon discovered that if you’ve different compiler vendors too, then lord have mercy on your soul!)
So, some JavaFX to prove our case, using that Groovy class.
$ cat fxUsesG.fx
for (l in [new lettersG().getLetters()])
print ("<{l}>");
Quick compile
$ javafxc.exe fxUsesG.fx
Quick test
$ javafx.exe -cp ".;./groovy-all-1.6.0.jar" fxUsesG
Output: <a><b><c><d><e><f><g><h><i><j><k><l><m><n><o><p><q><r><s><t><u><v><w><x><y><z>
Yes, isn’t that just so easy? Well, yes and no. Yes I have shown that one can write fantastically brief and clean code, something that will promote
design as the focus, and not code detail.
The no? Burn these two languages into the grey matter first before ‘mixing it up’ – fail to know your weapon at your own peril!
Conclusion: I’ve read much about people wondering why JavaFX was created. A lot of negative feedback in fact. I feel like it might be “looking a gift horse in the mouth” – I want both easy coding and beautiful looking product! Now I don’t have to return to C++ for the latter. And one would never do that for the former! One needs to practice these languages to avoid being tripped up by the subtleties. I had some fun with ( ) and [ ]. Try switching them (or should I say mis-using them) in both Groovy and JavaFX to see what errors you get or what alternative types you generate!
My final words, for many the discomfort of learning and remembering the rules for 2 extra languages may well be too much. Maybe not the “marriage made in heaven”. I am sure that the Groovy guys are seeing how they can make best use of the new JavaFX libraries. Maybe one will eventually get the improved GUI look via Groovy. But for now, the two complement each other. And so this little post on marrying the two.
Alex Garrett: Designer, coder, ranter and writer at www.lexecorp.com
Often seen working the crowds (promotions, soliciting, begging,…) but mostly freelance coding for a meager living.




I think you could make a good case for a sweet JavaFX GUI with a Groovy back-end. A match made in some sort of heaven-ish place :)
Pingback: Language success via TIOBE « LEXECORP