2017-03-01 68 views
0

什麼時候TableCell中的updateItem()方法被調用? 與該單元格關聯的屬性是否發生變化?javafx中的updateItem()方法TableCell類

在我的應用程序中,我有一個線程可以根據提供的超鏈接下載內容。我有一個TableView,它在兩個不同的列中顯示下載的名稱和進度。在進度列中,我想要一個進度條和一個標籤顯示已下載%的進度條的中心。爲此,我從Progressbar and label in tablecell獲得了幫助。但似乎updateItem()方法未讀取「進度」變量,每次都讀取-1。通過增加它的下載字節數

Progress.setCellValueFactory(new PropertyValueFactory<Download, Double>("progress")); 
     Progress.setCellFactory(new Callback<TableColumn<Download, Double>, TableCell<Download, Double>>() { 
      @Override 
      public TableCell<Download, Double> call(TableColumn<Download, Double> param) { 
       return new TableCell<Download, Double>(){ 
        ProgressBar bar=new ProgressBar(); 
        public void updateItem(Double progress,boolean empty){ 
         if(empty){ 
          System.out.println("Empty"); 
          setText(null); 
          setGraphic(null); 
         } 
         else{ 
          System.out.println(progress); 
          bar.setProgress(progress); 
          setText(progress.toString()); 
          setGraphic(bar); 
          setContentDisplay(ContentDisplay.GRAPHIC_ONLY); 
         } 
        } 
       }; 
      } 
     }); 

ADT我的下載類

public class Download extends Task<Void>{ 
    private String url; 
    public Double progress; 
    private int filesize; 
    private STATE state; 
    private Observer observer; 
    public Object monitor; 
    private String ThreadName; 
    private int id; 

    public static enum STATE{ 
     DOWNLOADING,PAUSE,STOP; 
    } 
    public Download(String url,Observer observer,Object monitor){ 
     this.url=url; 
     this.observer=observer; 
     this.monitor=monitor; 
     progress=new Double(0.0d); 
    } 

在下載類,我不斷更新「進步」變量的run方法。

回答

1

Task中有一個progress屬性,但如果您寫入所添加的進度字段,則不會修改它。 (PropertyValueFactory使用方法來檢索結果,而不是字段和Double字段不提供一種方式來反正觀察它。)

updateProgress應該用於更新該屬性,以保證物業正常使用應用程序線程同步。

例如

public class Download extends Task<Void>{ 

    protected Void call() { 

     while (!(isCancelled() || downloadComplete())) { 

       ... 

       // update the progress 
       updateProgress(currentWorkDone/totalWork); 
     } 

     return null; 
    } 

}