Some JavaFx/Java/Groovy examples - No Fluff Just Stuff

Some JavaFx/Java/Groovy examples

Posted by: Andres Almiray on December 4, 2008

You probably know by now that JavaFX SDK 1.0 was released today, as a result I downloaded it and followed Weiqi's instructions to access it on Linux. Why you ask? to be able to update jSilhouette of course! But before getting into that I wanted to test Java/Groovy integration with JavaFX. The following code contains no shiny demos nor amazing graphics but it may give you an insight on the inter-relationships between the 3 languages.

OK, first we need a test subject, a simple POJO (Poxo? Pojfxo? as a matter of fact JavaFX classes are everything but plain, they are actually a composite of two classes, more on that later) will suffice.
A very simple example really, the class has a property and a business method, that's all. One think that irks me is that you have to import java.lang.System, but that may be understandable since JavaFX != Java and JavaFX's mobile profile must save every bit as space is limited. This is what you get when running the example

[aalmiray@localhost javafx]$ javafxc Ball.fx
[aalmiray@localhost javafx]$ javafx Ball
Bouncing 3.0 times

Remember that I mentioned JavaFX classes are actually composite classes?

[aalmiray@localhost javafx]$ ls Ball*
Ball.class
Ball.fx
Ball$Intf.class

JavaFX classes are compiled to Java byte code, which means you can inspect their contents with any regular Java tool, say javap for example

[aalmiray@localhost javafx]$ javap Ball
Compiled from "Ball.fx"
class Ball extends java.lang.Object implements Ball$Intf,com.sun.javafx.runtime.FXObject{
  public double $Ball$size;
  public boolean $Ball$size$needs_default$;
  public static Ball$Intf $ball;
  public static java.lang.Object javafx$run$(com.sun.javafx.runtime.sequence.Sequence);
  public static void bounce$impl(Ball$Intf, double);
  public double get$Ball$size();
  public double set$Ball$size(double);
  public static void applyDefaults$Ball$size(Ball$Intf);
  public void initialize$();
  public static void addTriggers$(Ball$Intf);
  public void bounce(double);
  public Ball();
  public Ball(boolean);
  public static void userInit$(Ball$Intf);
  public static void postInit$(Ball$Intf);
  public static void main(java.lang.String[]) throws java.lang.Throwable;
  static {};
}

Plenty of stuff there, notice the naming convention for the size property accessor, it may be hard to call it from Java unless you resort to reflection tricks; on the other hand the bounce method looks like an easy target to be called from Java without too much fuzz, let's try it out shall we?
Again keeping things simple. Notice there are no imports of any JavaFX classes, let's pray this works

[aalmiray@localhost javafx]$ javac -cp /usr/local/javafx/lib/shared/javafxrt.jar:. JBall.java
[aalmiray@localhost javafx]$ java -cp /usr/local/javafx/lib/shared/javafxrt.jar:. JBall
Bouncing 4.0 times

It works! no need for a cumbersome bridge (yes, I'm looking at you javax.script, watch it!) to call the JavaFX class. Now property access is a different matter altogether, and let's face it, using Java Reflection on its own is rather ugly, too many exceptions need to be caught! (ugh) let's switch to Groovy instead and see what we got. First let's replicate the Java scenario with Groovy to get a base starting point
Don't you love Groovy? I know I do :-D here goes the output

[aalmiray@localhost javafx]$ groovy -cp /usr/local/javafx/lib/shared/javafxrt.jar:. GBall.groovy
Bouncing 5.0 times

We're ready to get funky with some meta-programming. As shown previously the Ball class exposes its size property as $Ball$size so all we have to do is intercept all property access (get/set) and route it to the correct method call. Luckily for us ExpandoMetaClass makes this task dead simple as shown here
Note the implicit return on the if/else clauses, this is one of the recent additions to the groovy 1.6-beta series. If everything goes according to plan we should see a lists of properties coming from the ball instance, then the current value of the ball's size property (10), lastly updating it to 20 and print the value again, here goes

[aalmiray@localhost javafx]$ groovy -cp /usr/local/javafx/lib/shared/javafxrt.jar:. GBall.groovy
Bouncing 5.0 times
class -> class Ball
$Ball$size -> 10.0
10.0
20.0

And surely it works! Who said Groovy doesn't simply things ;-)

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 »