Taking SwingBuilder's binding for a spin - No Fluff Just Stuff

Taking SwingBuilder's binding for a spin

Posted by: Andres Almiray on August 20, 2008

Those of you that follow @shemnon's blog will surely know that he has revamped how binding is supported in the development versions of Groovy 1.6.x (here, here and here). Impatiently as ever, I created my own distro of groovy 1.6-beta-2-SNAPSHOT and gave it a try, quite fitting with the latest JavaFx Script experiments.

One of the appealing features of JavaFx Script is that it has binding baked right into the language, it also sports the notion of triggers. You can attach any function on a trigger, whenever the trigger receives a binding update event the function will be called. This is akin to PropertyChangeListener and PropertyChangeEvent, only you don't see those classes in the open with JavaFx. So here goes, three simple binding/triggers examples on JavaFx Script and Groovy. The examples are not comprehensive, binding in both languages can cover a broader spectrum. It is important to note that binding in Groovy is provided by a set of APIs (SwingBuilder and the bind factory) not by the language itself.

The first snippet exemplifies binding between two components, the label's text will be updated every time text is written on the textField, first comes the JavaFx Script version
And now its Groovy counterpart

Maybe there is another way to setup the binding between the label and the textfield on JavaFx Script, but you can't define variables while building the content unless you do it inside a function, this means that those components are not built close to the place where they are inserted into the UI hierarchy. Clearly that is not a problem with the Groovy version. The next snippet introduces one level of indirection, a textModel. Now the label will bind to a third object that holds a text attribute. A button is also added to 'trigger' the update
The code is pretty much self explanatory, I would say the same holds true for the Groovy versionObservableMap is an utility class that fires up PropertyChangeEvents every time a property is added, updated and/or removed from it, in contrast observable beans usually fire events when a property is updated only (more options for you!). I can hear someone at the back saying 'but JavaFx Script does have triggers, why don't you use them instead?' certainly I will. So let's remove the button and bind the label, textField and textModel, adding a console printout just to show that the model is being updated as expected, and it in turn updates the labelIt only took two binding calls and one trigger, sweet. What about the Groovy version? well, we don't have triggers (yet) that make the code short but it is certainly doableNotice that a regular PropertyChangeListener has been registered with the ObservableMap, this is how triggers are implemente in Java/Groovy. Binding the textField's text to the textModel's text property is also a bit more verbose than before. As a matter of fact that syntax used to be the previous one, so you can see that binding has been improved, just a bit but good enough :-D I think it is possible to cook up a trigger dsl to reduce the amount of code required to create triggers in Groovy, don't you think? we have metaprogramming on our side after all.

Keep on Groovying!
Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »