Over a million developers have joined DZone.

How Groovy Helps JavaFX: Farewell Pure Java Code?

DZone's Guide to

How Groovy Helps JavaFX: Farewell Pure Java Code?

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

One of the many cool sample applications known to those trying out JavaFX is the JavaFX Weather application, which is now bundled with the NetBeans IDE 6.5.1/JavaFX 1.2 bundle. In short, it connects to a weather service and then displays the results for selected cities in an impressive JavaFX GUI:

In a technical session at JavaOne on Wednesday, entitled "JavaFX Programming Language + Groovy = Beauty + Productivity", Dierk König showed a number of powerful ways in which Groovy and JavaFX can interact. One of those ways is outlined below, with all the code and the results. It is really quite impressive and earned Dierk a round of applause from his audience when he demoed it in his session.

The JavaFX Weather application creates the above GUI, while using this massive Java class to connect to Yahoo's weather service. The RSS feed that the Java class connects to is displayed in full below:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">


        <title>Yahoo! Weather - Prague, EZ</title>
        <description>Yahoo! Weather for Prague, EZ</description>
        <lastBuildDate>Fri, 05 Jun 2009 8:00 pm CEST</lastBuildDate>

        <yweather:location city="Prague" region=""   country="EZ"/>
        <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
        <yweather:wind chill="54"   direction="60"   speed="3" />
        <yweather:atmosphere humidity="58"  visibility="6.21"  pressure="29.8"  rising="0" />
        <yweather:astronomy sunrise="4:56 am"   sunset="9:06 pm"/>

            <title>Yahoo! Weather</title>


            <title>Conditions for Prague, EZ at 8:00 pm CEST</title>
            <pubDate>Fri, 05 Jun 2009 8:00 pm CEST</pubDate>
            <yweather:condition  text="Partly Cloudy"  code="30"  temp="54"  date="Fri, 05 Jun 2009 8:00 pm CEST" />
<img src="http://l.yimg.com/a/i/us/we/52/30.gif"/>
<b>Current Conditions:</b>
Partly Cloudy, 54 F
Fri - Partly Cloudy. High: 58 Low: 42
Sat - PM Rain. High: 58 Low: 49
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Prague__EZ/*http://weather.yahoo.com/forecast/EZXX0012_f.html">Full Forecast at Yahoo! Weather</a>(provided by The Weather Channel)<br/>
            <yweather:forecast day="Fri" date="5 Jun 2009" low="42" high="58" text="Partly Cloudy" code="29" />
            <yweather:forecast day="Sat" date="6 Jun 2009" low="49" high="58" text="PM Rain" code="12" />
            <guid isPermaLink="false">EZXX0012_2009_06_05_20_00_CEST</guid>




Now, whenever you hear "Groovy", you should think "grunt work". That's what Groovy is particularly good at. A very strong case in point is that of web services. Also, that of parsing HTML and XML. Therefore, when you need to interact with web services in your Java application, the most obvious helper language you should think of is Groovy.

Look again at the RSS above and then look at line 11 in the Groovy code below. Here's that line:

def channel = new XmlParser().parse(url).channel
That line gets you the "channel" element in the RSS feed above! Awesome, right? And from there on, the Groovy script below parses the RSS feed, identifying exactly those pieces that are of interest to the JavaFX GUI, resulting in EXACTLY the same result, in approximately 20 lines, as the original does in approximately 250.

Take a look at the Groovy snippet below to see how it works, by comparing it to the RSS feed above. Note that this isn't even a snippet! It is the WHOLE Groovy web service class. Now that's just plain cool, especially after comparing it again to the original monstrosity.

package weatherfx.service

class YahooWeatherServiceG {
static YW = new groovy.xml.Namespace("http://xml.weather.yahoo.com/ns/rss/1.0")

def forecasts

YahooWeatherServiceG(String code, boolean celsius) {
def url = "http://weather.yahooapis.com/forecastrss?u=f&p=$code"
println url.toURL().text
def channel = new XmlParser().parse(url).channel
cityName = channel[YW.location].@city
def wind = channel[YW.wind].first()
windSpeed = wind.@speed.toInteger()
windDirection = wind.@direction.toInteger()
def cond = channel.item[YW.condition].first()
temp = cond.@temp.toInteger()
forecasts = channel.item[YW.forecast]

String cityName
int temp
int windSpeed
int windDirection

int getConditionCode(int day=0) { forecasts[day].@code.toInteger() }
int getLowsTemp (int day=0) { forecasts[day].@low.toInteger() }
int getHighsTemp (int day=0) { forecasts[day].@high.toInteger() }

Now, take a moment to imagine how much simpler, effective, and less error-prone it will be to (a) test and (b) maintain the above code, compared to its original pure Java version.

However, there isn't a JavaFX/Groovy cross-compiler yet. So, how to replace your Java web service code with the above in Groovy? Create a separate project in which you create your Groovy web service. Then add that project to your JavaFX application's classpath. Next, replace the two or three references in your JavaFX application to refer to the Groovy class (which, after compilation, is now a Java class) instead of the original Java class.

In a single picture, the above paragraph gets you the following:

Then run the JavaFX application and you'll have the same result as before, with the difference that the web service code is now handled in Groovy. And... there's no pure Java code in your application at all anymore. Oh, dear. JavaFX creates the GUI, while Groovy does the grunt work in the background. So, farewell, pure Java code?


Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}