FxBuilder Update 2 - No Fluff Just Stuff

FxBuilder Update 2

Posted by: Andres Almiray on June 22, 2009

Bridging the gap between JavaFX widgets/controls and Griffon based applications is not the only feature provided by FxBuilder (here is a small summary of what it can do right now in that regard), it is also able to bridge the gap between languages, all thanks to Groovy's metaprogramming facilities.

For instance, properties defined on a JavaFX class have a distinct signature that is similar and at the same time alien, if compared to the JavaBean conventions followed by Java/Groovy, the following JavaFX Script class
when compiled with the current JavaFx 1.2 compiler produces the following bytecode signatures
As you can see the str variable has a getter/setter pair but the names are a bit different to what we are used to in Java/Groovy land, there is also an additional related field loc$str, which by my understanding, is what makes binding work. Another funny bit about loc$str is that it is lazy initialized, it usually is linked to str, but that can change depending on the access modifier set on str. In any case I think you agree that calling
from either Java or Groovy is not that pleasant, what about this
it looks like JavaFX Script code but it is actually Groovy code, FxBuilder makes sure to weave the necessary meta-magic for you, no need to dive into JavaFX's internals. Back to ObjectLocation, which makes binding tick, instances of that class may have listeners attached to them, but those listeners are not your typical PropertyChangelisteners so you're forced to attach a different type of listener: com.sun.javafx.runtime.location.ChangeListener. If the package name sends a red alert through your system you'll be correct, this is an internal construct, as a matter of fact it used to be called ObjectChangeListener and it had a simpler API. Well FxBuilder says "don't worry, I'll handle that complexity for you" by letting you register either a Closure or a PropertyChangeListener as you're used to
There are other useful bits of syntactic sugar and shortcuts:
  • All FXObjects have
    • location(String name) - returns an ObjectVariable identified by name
    • hasLocation(String name) - does the object contain an attribute identified by name?
    • locationType(String name) - returns the type of the ObjectVariable identified by name
  • Sequences can be accessed using array subscript notation because they haven been enhanced with getAt(int).
  • You can register Changelisteners directly to ObjectVariables using a closure, example: hw.location("str").onChange = { oldValue, newValue -> /*do something*/ }
More shortcuts will be added as the need arises.

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 »