JavaFx AsyncTask


License

License

The New BSD License
GroupId

GroupId

com.victorlaerte
ArtifactId

ArtifactId

jfx-asynctask
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

pom
Description

Description

JavaFx AsyncTask
JavaFx AsyncTask
Project URL

Project URL

https://github.com/victorlaerte/javafx-asynctask
Project Organization

Project Organization

Victor Oliveira
Source Code Management

Source Code Management

https://github.com/victorlaerte/javafx-asynctask

Download jfx-asynctask

How to add to project

<!-- https://jarcasting.com/artifacts/com.victorlaerte/jfx-asynctask/ -->
<dependency>
    <groupId>com.victorlaerte</groupId>
    <artifactId>jfx-asynctask</artifactId>
    <version>1.0.1</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/com.victorlaerte/jfx-asynctask/
implementation 'com.victorlaerte:jfx-asynctask:1.0.1'
// https://jarcasting.com/artifacts/com.victorlaerte/jfx-asynctask/
implementation ("com.victorlaerte:jfx-asynctask:1.0.1")
'com.victorlaerte:jfx-asynctask:pom:1.0.1'
<dependency org="com.victorlaerte" name="jfx-asynctask" rev="1.0.1">
  <artifact name="jfx-asynctask" type="pom" />
</dependency>
@Grapes(
@Grab(group='com.victorlaerte', module='jfx-asynctask', version='1.0.1')
)
libraryDependencies += "com.victorlaerte" % "jfx-asynctask" % "1.0.1"
[com.victorlaerte/jfx-asynctask "1.0.1"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Travis-CI Open Source Love

JavaFx AsyncTask

This class was created to simplify how to handle Thread tasks in Javafx, and it is based on the same idea of AsyncTask from Android.

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by extending the class, and 4 steps, called onPreExecute, doInBackground, progressCallback and onPostExecute.

Optionally you have the method setDaemon to set your threads daemon, which means that if your javafx application has been closed it can still running or not. setDamon(boolean) can only be called before the thread has been started. By default the thread is set to daemon.

Use

Maven

<dependency>
    <groupId>com.victorlaerte</groupId>
    <artifactId>jfx-asynctask</artifactId>
    <version>1.0.1</version>
</dependency>

Gradle

compile group: 'com.victorlaerte', name: 'jfx-asynctask', version: '1.0.1'

Methods

onPreExecute - is used to run some rotine before the background task has started

doInBackground - is used to perform background tasks

onPostExecute - is used to run some finally rotine after background task has done

progressCallback - it will be called every time you call publishProgress to update your UI Thread as you want

publishProgress - is used to call your progressCallback and update your UI component

setDaemon - is used to set your thread daemon

execute - called to initiate all process

interrupt - is called to interrupt your thread process

Example of usage:

public class Example extends AsyncTask {
    private UIController controller;

    public Example(UIController controller) {
        this.controller = controller;
    }

    @Override
    void onPreExecute() {
    
        //This method runs on UI Thread before background task has started
        this.controller.updateProgressLabel("Starting Download")
    }

    @Override
    void doInBackground() {

    //This method runs on background thread
    
    boolean downloading = true;
    
        while (downloading){
        
            /*
            * Your download code
            */
            
            double progress = 65.5 //Your progress calculation 
            publishProgress(progress);
        }
    }

    @Override
    void onPostExecute() {

        //This method runs on UI Thread after background task has done
        this.controller.updateProgressLabel("Download is Done");

    }

    @Override
    void progressCallback(Object... params) {
    
        //This method update your UI Thread during the execution of background thread
        
        double progress = (double)params[0]
        this.controller.updateProgress(progress);
    }
}

//To call this class you just need to instatiate that doing 

Example testing = new Example(myController);
testing.execute();

Sample here

Versions

Version
1.0.1
1.0.0