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.

No comments:

Post a Comment