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.

Profile: desktop

Variable Summary

accessnametypeCan ReadCan InitCan WriteDefault Valuedescription
public-readcancelledBoolean

A read-only variable that indicates if this worker has been cancelled by calling the cancel() function.

A read-only variable that indicates if this worker has been cancelled by calling the cancel() function. Should be used to break out of any background processing loops so the task ends promptly.

 
public-init protectedinBackgroundfunction():Objectsubclass

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.

 
public-init protectedonDonefunction(:Object):Voidsubclass

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.

 
public-init protectedonFailurefunction(:ExecutionException):Voidsubclass

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-init protectedprocessfunction(:Sequence):Voidsubclass

This function is called on the event dispatch thread to process incremental data from the publish method.

This function is called on the event dispatch thread to process incremental data from the publish method. Multiple invocations of publish may occur before this method is called, and the list of data passed in will be the aggregated values from all the publish calls.

Since this function is guaranteed to be called on the Event Dispatch thread, you can safely make changes to the UI state.

 
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.

workerObjectSwingWorker

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.

 
processProxy(data: java.lang.Object[]) : Void
Parameters
data
 
public publish(data: java.lang.Object[]) : Void

Allows data to be incrementally processed on the event dispatch thread.

Allows data to be incrementally processed on the event dispatch thread. This function is safe to call from inBackground, and will pass an aggregated sequence of data to the process method.

Parameters
data
 
start() : Void
 

Inherited Functions