简体   繁体   中英

JavaFX execute code after the Task is completed

I'm using JavaFX for my application and I'm using Task from JavaFX to upload files.

public class Upload extends Task<UploadFile> {  
   private UploadFile uploadFile;     
   public Upload(UploadFile uploadFile){
        this.uploadFile= uploadFile;
    }

    @Override
    protected synchronized UploadFile call() throws Exception {
        ....
            hcl.invokeMethodUploadFile(uploadFile);
            return null;
    }
}

The method "initiate" creates an UploadFile-Container and adds it to the queue of the Threadpoolexecutor. This method will be executed for every file once. Also this class contains additional and necessary information to proceed after the file upload was completed.

public void initiate(){

        UploadFile uc = new UploadFile(file);
        // Set other informations as well

   // add Container to queue
        main.getUploadFiles().add(uc);

    } 

UploadFile is a class containing information about the file, serverinformation, etc.
Amongst others i want to insert the url into a database after the upload is completed. How can I execute some code after the file was uploaded?

When you create the task, which you presumably do somewhere with

Upload upload = new Upload(uc);

you can do

upload.setOnSucceeded(e -> {
    // this code executed when task is successfully completed
    // this is executed on the FX Application Thread, so you can
    // safely modify the UI
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM