2017-01-16 141 views
0

我爲我的應用程序使用JavaFX,並使用JavaFX中的任務來上傳文件。任務完成後JavaFX執行代碼

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; 
    } 
} 

「啓動」方法創建一個UploadFile容器並將其添加到Threadpoolexecutor的隊列中。該方法將爲每個文件執行一次。此類還包含在完成文件上傳後繼續執行的其他必要信息。

public void initiate(){ 

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

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

    } 

UploadFile是一個包含文件,serverinformation信息類等
除其他我要將網址插入到數據庫上傳完成後。如何在文件上傳後執行一些代碼?

+1

爲什麼你的任務返回空,如果有'UploadFile'的返回類型? –

+0

這是我的代碼中的一個錯誤,注意到它前幾個幾秒,並將其更改爲void – Ronon

+0

現在它不會編譯。如果它不應該返回任何內容,則使其延伸到任務並返回null。 –

回答

4

當你創建任務,你可能與

Upload upload = new Upload(uc); 

做的地方,你可以做

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 
});