| By James L. Weaver | Article Rating: |
|
| November 22, 2007 12:00 PM EST | Reads: |
11,215 |
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.
Published November 22, 2007 Reads 11,215
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By 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 01/22/08 02:00:23 PM EST | |||
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 01/20/08 02:42:12 AM EST | |||
Aaron, * 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 * 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, |
||||
![]() |
Aaron Romine 01/18/08 12:38:56 PM EST | |||
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? |
||||
- An Exclusive Interview with Oracle, Cloud Expo 2010 Diamond Sponsor
- Whatever the Apple iPad Is, It Apparently Leaks Like a Sieve
- Whatever Happened to JAAS?
- What’s Next for Oracle-Sun?
- Cloud Expo New York Call for Papers to Expire January 15, 2010
- Six Enterprise Megatrends to Watch in 2010
- Oracle Maps Its Cloud Computing Strategy During Cloud Expo Keynote
- Oracle’s Next Sun Hurdle
- Oracle Claims Victory Over EC; Says Sun Will Sell Clouds
- Now Russia Threatens to Hold Up Oracle-Sun Deal
- Free Virtual Appliance for Cloud Computing
- Why Cops and Java Developers Have Low Salaries?
- Kindle 2 vs Nook
- Cloud Expo New York Call for Papers Now Open
- Is Cloud Computing Like Teenage Sex?
- An Exclusive Interview with Oracle, Cloud Expo 2010 Diamond Sponsor
- Performance Tuning Essentials for Java
- Whatever the Apple iPad Is, It Apparently Leaks Like a Sieve
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Whatever Happened to JAAS?
- Cloud Computing Can Revitalize Your Career as Software Developer
- What’s Next for Oracle-Sun?
- Cloud Expo New York Call for Papers to Expire January 15, 2010
- The End of IT 1.0 As We Know It Has Begun
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?


























