Thursday, October 8, 2009

Creating Asynchronous Task In JavaFX

Here is a example to create asynchronous task in JavaFX.
To create an asynchronous task in JavaFX you need to do following steps:


  1. Create an interface with your callback method.

  2. Create a Java class that implements javafx.async.RunnableFuture and implement its run method. Store reference of your interface in order to call declared callback method.

  3. Create a JavaFX class that extends javafx.async.JavaTaskBase and implement create method. This method returns the object of your javafx.async.RunnableFuture. You have to create object of the class which implements javafx.async.RunnableFuture and return it.

  4. Pass object reference which is of your interface type.

  5. Create Object of javafx.async.JavaTaskBase class and call start function.

Look at the following example.
Suppose there is a condition where I want my object to be nitified after given interval. I can achieve this by following code.
###################################################################
Write an Interface.
###################################################################
package timerdemo;
/**
*
* @author deepak_kalra
*/
public interface MyTimer {
public void timeUp();
}
###################################################################
Write a Thread class.
###################################################################
package timerdemo;
import com.sun.javafx.functions.Function0;
import javafx.async.RunnableFuture;
/**
*
* @author deepak_kalra
*/
public class MyTimerThread implements RunnableFuture {
private int sleepTime;; private MyTimer timer;
public MyTimerThread(MyTimer timer) {
this(timer, 1000);
}
public MyTimerThread(MyTimer timer, int sleepTime) {
this.timer = timer;
this.sleepTime = sleepTime;
}
@Override
public void run() {
try {
Thread.sleep(sleepTime);
javafx.lang.FX.deferAction(new Function0()
{
@Override
public Void invoke() {
timer.timeUp();
return null;
}
});
}
catch(InterruptedException ie) {
ie.printStackTrace();
}
}
}
###################################################################
Write a Task class.
###################################################################
package timerdemo;
import javafx.async.JavaTaskBase;
import javafx.async.RunnableFuture;import simplecalculator.MyTimerThread;
/**
* @author deepak_kalra
*/
public class MyJavaFXTask extends JavaTaskBase, MyTimer {
public override function create():RunnableFuture {
var t = new MyTimerThread(this, 1000);
return t;
}
public override function timeUp():Void {
println("Hey! Time is up buddy.");
}
}
################################################################### Write a Main class. ###################################################################
package timerdemo;
/**
* @author deepak_kalra
*/
function run(args : String[]) {
println("Before starting timer.");
var task = new MyJavaFXTask();
task.start();
println("After starting timer.")
}
################################################################### Output:
Before starting timer.
After starting timer.
Hey! Time is up buddy.

Wednesday, October 7, 2009

JavaFX New Beginning

Good user interface is an important factor for application success. User now a days want rich and interactive UI. Multimedia content is more preferred over static non interactive content. User want interactive content full of animation and special effects. With some jazzy features like audio running at the back, images and text moving around the screen etc.

Creating such rich user interface is not an easy task and is not achievable using normal programming language. Special software's, language's are coming in the market which can be used to create such interface.

You might have seen various website's having animated intro movie kind of stuff running on there website. It is a flash file which run in a flash player which is normally installed on every machine & browser now a day's.

JavaFX is a step from SUN in the same direction. It means it is used to create RIA (Rich Internet Applications). JavaFX is focused for content creators.

JavaFX runs on top of JVM. JavaFX is a scripting and declarative language.
Benefits:
Easy to learn.
Ready to deploy.
Inbuilt support for multimedia.
Cost Effective.
Data Binding.

What can you make:
Games, Animation Movies, Widgets, Web UI etc.

To start learning JavaFX I suggest you to download latest Netbeans IDE for JavaFX.

Also you can visit JavaFX official website for more information. http://www.javafx.com/