JavaFX: Bringing Rich Experiences To All the Screens Of Your Life

Profile: desktop, common

Overview

JavaFXWorker is a SwingWorker equivalent for JavaFX Script to allow execution of asynchronous operations in the background with notification and completion callbacks on the Event Dispatch Thread (EDT).

Here is an example that will load an Image and set it to a variable when the load is completed:

 var currentImage:Image;
 var worker = JavaFXWorker {
     inBackground: function() {
         return Image {url: currentFile.toURL().toString(), height: imageHeight};
     }
     onDone: function(result) {
         currentImage = result as Image;
     }
 }
 

Both the inBackground and onDone handlers are required to be set. Upon initialization the worker will automatically start execution on a background thread and can be later stopped by calling cancel. Any results of execution in the background thread will be saved to the result var and also passed in to the onDone handler.

Variable Summary

accessnametypeCan ReadCan InitCan WriteDefault Valuedescription
public-initinBackgroundfunction():Object

Function that will be executed on a background thread.

Function that will be executed on a background thread. If an exception is thrown while executing this function the failed var will be set to true and failedText will be set to the exception message.

Since this method is executed asynchronous to other UI operations, it is not safe to make calls that will modify the UI state. This includes most JavaFX Script library operations.

Failure to set this var will result in an NPE.

 
public-initonDonefunction(:Object):Void

Function that will be called once inBackground completes.

Function that will be called once inBackground completes. The result of the background function will be passed in to the result parameter when this function is called.

This function is guaranteed to be called on the Event Dispatch thread, and can safely make changes to the UI state.

Failure to set this attribute will result in an NPE.

 
public-initonFailurefunction(:ExecutionException):Void

Function that will be called if inBackground fails due to an exception.

Function that will be called if inBackground fails due to an exception. The exception is passed in as a parameter when this function is called, and the attributes failed and failureText will be set to 'true' and the message of the exception, respectively.

This function is guaranteed to be called on the Event Dispatch thread, and can safely make changes to the UI state.

This var may be left null if no special handling of exceptions is required.

 
public-readresultObject

This var gets set to the result returned by the inBackground method if it is successful, and will also be passed in to the onDone handler.

Inherited Variables

Function Summary

public cancel() : Void

Immediately cancels the background thread if it is executing by throwing an interruped exception.

Immediately cancels the background thread if it is executing by throwing an interruped exception.

 

Inherited Functions