YOUR FEEDBACK
Three RIA Platforms Compared: Adobe Flex, Google Web Toolkit, and OpenLaszlo
NN wrote: Yeah you are right GWT is poor man's Flex. After using GWT on two...


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


Closures in Compiled JavaFX Script
One of the very useful (and cool) features of compiled JavaFX Script will be closures

Digg This!

From Jim Weaver's Learn JavaFX Weblog

One of the very useful (and cool) features of compiled JavaFX Script will be closures.  In a nutshell, JavaFX Script closures provide the ability to define a function within another function with the inner function having access to the local variables of the outer function.  This feature is enabled by the fact that in compiled JavaFX Script, functions are first-class objects, which provides the ability to assign functions to variables and to pass functions as arguments to other functions.  You can read more about closures in this Wikipedia article.

Closures are often used to implement callback functions, many times handling UI events.  Since the UI part of compiled JavaFX Script isn't ready for prime-time yet, I'd like to show you a callback example in which a function is called when a timer expires.  You can run this example by downloading and building the JavaFX Compiler in its current state from the Subversion repository in the OpenJFX Compiler Project

Here's the source code for the example:

/*
*  ClosureExample.fx - Demonstrates closures, and that
* functions are first-class
*  objects in compiled JavaFX Script.
*
*  Uses JavaFX Script compiled version
*  Developed 2007 by James L. Weaver
* (jim.weaver at lat-inc dot com)
*/

import java.lang.System;
import java.lang.Thread;

public class ClosureExample {

  function startTimer(millis:Integer,
wakeUp:function():Void):Void {
    Thread.currentThread().sleep(millis);
    wakeUp();
  }
 
  function setAlarm(sleepTime:Integer):Void {
    var message = "Time to wake up!";
    var wakeUpFunc =
      function() {
        System.out.println(message);
        System.out.println("You slept
for {sleepTime} milliseconds");
      };
    startTimer(sleepTime, wakeUpFunc);
   
  }
}

var example =
  ClosureExample {
  };
 
example.setAlarm(3000);

Walking Through the Closure Related Features of this Example

After creating an instance of the ClosureExample class, and calling its setAlarm() function, you'll notice that an anonymous function is defined and assigned to a variable named wakeUpFunc.  After that, the startTimer() function is invoked, passing the desired duration of the timer, as well as the wakeUpFunc reference.

Within the startTimer() function, the sleep() method of the Java Thread class is called, which causes the current thread to sleep for the desired number of milliseconds.  This, by the way, demonstrates JavaFX Script's ability to leverage the power of Java classes (see the Putting My CTO Hat On post).  After the thread wakes up, the callback function that was passed into the startTimer() function is invoked, which performs the closure.

The net effect is that the callback function accesses and prints the values of local variables of the setAlarm() function.  The closure feature allows access to these variables from within the scope of the callback function, even though they're not local to the callback function.

About James L. Weaver
James L. (Jim) Weaver is founder and president of jMentor, formed in 2000 to provide Java programming-related training to companies and individuals. He has served as a system architect and developer for over 25 years, specializing in leading-edge software development. His specialties include Java, object-oriented, and web-based technologies. He has authored books on the Java programming language, including most recently JavaFX Script, published by Apress.

Aaron Romine wrote: I actually stumbled on this article as I was searching about JavaFX because someone mentioned that JavaFX didn't support inner functions. In my googling I came across this. Good to see that isn't the case. Thanks for the list and link. We haven't had a need for JavaFX yet, but I always like to keep the language option open. Thanks!
read & respond »
Jim Weaver wrote: Aaron, Good to hear from you. It's interesting that you would respond to this particular post, as I recall that you first introduced closures to me (in the context of JavaScript). The major benefits that I see with JavaFX Script are: * declaratively expressing the UI, including layout widgets * binding the UI to a model, and its innate ability to do the MVC pattern * attribute triggers * leveraging Java classes and compiling to JVM bytecode * excellent 2D graphics, and soon to come animation, support. 3D graphics will follow. All of the above are implemented in a simple, elegant, and fun to use way. Please see my [http ://learnjavafx.typepad.co m/weblog/2007/10/putting- my-cto-.html Putting My CTO Hat On] post for more details on my rationale for utilizing JavaFX Script. Thanks, Jim Weaver "Helping You Become a JavaFXpert" weblog: http://javafxpert.com
read & respond »
Aaron Romine wrote: Nice article. I'm just starting to look at JavaFX and want to know the benefits of use it verse something like Rhino. It seems that Rhino offers many of the same capabilities (using native java classes) and is already integrated with some projects, like Apache Cocoon. Rhino being JavaScript, I am more familiar with it. Other than JavaFX being strongly typed, what other benefits does JavaFX provide?
read & respond »
LATEST JAVA STORIES & POSTS
Chris Keene's Prescription for Curing the Java Flu
At WaveMaker, we have hitched our wagon to Java so I hope very much that JavaOne is showing us the ghost of Java present, not the ghost of Java to come. The Sun promise to put Java runtimes everywhere is meaningless if nobody wants to develop for those runtimes. Adobe and Microso
Virtualization Journal Attracts JavaOne Attendees to SYS-CON Media Booth
Virtualization Journal now reaches more than 60,000 online readers with monthly digital editions and weekly newsletters. The premier issue of the magazine's print edition, which debuts on May 6, 2008, at JavaOne in San Francisco, as a media sponsor of this event, will be availabl
Real-Time Kaazing Solution and Sun's Glassfish Forge RIA Alliance
Kaazing Corporation and Sun Microsystems announced an alliance to deliver the scalable and advanced real-time Web 2.0 platform. The integration between Kaazing's real-time Rich Internet Application (RIA) solution, Enterprise Comet, and Sun Microsystems' open source Java EE applic
Sun Challenges Linux
Sun's mule train has finally pulled into Indiana after three years on the road. Indiana is the Linux-friendly Fedora-like OpenSolaris project meant to move the Solaris-shy Linux community off Linux and on to Solaris tempted by Solaris widgetry like the highly scalable, rollback-e
AJAX World - Sun Talks Up its Late-to-the-Party AIR-Silverlight Rival
At Java One this week Sun has been selling its year -old-but-still-upcoming - and definitely late-to-the-party - Adobe AIR- and Microsoft Silverlight-competitive JavaFX Rich Client environment as a potential revenue-generator capable of putting ads on mobile applications and JavaF
MySQL Backs Off Closed Source Plan
MySQL has backed off a plan to charge for some encryption and compression backup widgetry in the next version of the database - and, heavens, NOT OPEN SOURCE THE STUFF, an idea it trotted a few weeks ago and predictably caught hell for. Sun, which bought MySQL for a billion dolla
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE
BREAKING JAVA NEWS
Day Software to Present at Henry Stewart DAM Show
Day Software (SWX:DAYN) (OTCQX:DYIHY), a leading provider of global content management