YOUR FEEDBACK
johnpetersen wrote: Great post. You hit some good points, and hopefully me sending this post. It wil...


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
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

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.

YOUR FEEDBACK
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!
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.com/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
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?
LATEST JAVA STORIES & POSTS
JavaScript is pretty much everywhere you look these days, reaching far beyond your desktop browser. Adobe AIR lets you use JavaScript to create desktop installed HTML and AJAX apps. Apple uses it in its gadgets and in the iPhone's browser. And Nokia recently announced support for...
The Java Community Process (JCP) Program Management Office has announced the final results of the 2008 JCP Executive Committees (EC) elections. After two ballot rounds – for ratified and elected seats – the winners are Ericsson, SpringSource, SAP, Intel, and Werner Keil for t...
If you think your network is safe from the new strains of content security threats, think again. Today’s cybercriminals use sophisticated attacks that multiply quickly and thwart traditional defenses, rendering conventional security ineffective and unmanageable. To protect your...
Tidal Software has announced Intersperse 8.0, a product that monitors J2EE and .NET applications and their transaction component performance to produce meaningful metrics for managing applications and high-level business processes. The product leverages a combination of lightwei...
ILOG has announced ILOG JViews 8.5, the latest version of ILOG’s Java-based visualization suite, with new features that enhance the creation of Rich Internet Applications as well as desktop applications. ILOG JViews 8.5 adds support for the Eclipse platform including the new IL...
Emulex has announced that it will offer Fibre Channel Host Bus Adapters (HBA) and Fibre Channel over Ethernet (FCoE) Converged Network Adapters (CNAs) for use with OpenSolaris’ Common Multiprotocol SCSI Target (COMSTAR), thereby providing end-to-end Fibre Channel and FCoE suppo...
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

SPONSORED BY INFRAGISTICS
In every field of design one of the first things students do is learn from the work of others. They ...
There are many forces that influence technological evolution. After a decade of building enterprise ...
2008 is going to be an important year for Rich Internet Applications. Most organizations are deliver...
The OpenAjax Alliance is developing an Ajax industry wishlist for future browsers, using a dedicated...
Infragistics announced the availability of two Community Technology Preview (CTP) User Interface (UI...
The YUI development team has released version 2.5.2; you can download the new release from SourceFor...
ADS BY GOOGLE