[英]updateItem() method in javafx TableCell class
何時調用TableCell中的updateItem()方法? 是與該單元格關聯的屬性發生變化時嗎?
在我的應用程序中,我有一個線程根據提供的超鏈接下載內容。我有一個TableView,它在兩個不同的列中顯示下載的名稱和進度。在進度列中,我想在進度條的中心有一個進度條和一個標簽為此,我從Progressbar和tablecell的標簽中獲取幫助,但似乎updateItem()方法未讀取'progress'變量,並且每次都讀取-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);
}
在Download類的run方法中,我通過向其添加已下載字節數來不斷更新'progress'變量。
Task
有一個progress
屬性,但是如果您寫入添加的progress字段,則不會對其進行修改。 ( PropertyValueFactory
使用方法而不是字段來檢索結果,而Double
字段始終無法提供觀察結果的方法。)
應該使用updateProgress
來更新此屬性,以確保該屬性與應用程序線程正確同步。
例如
public class Download extends Task<Void>{
protected Void call() {
while (!(isCancelled() || downloadComplete())) {
...
// update the progress
updateProgress(currentWorkDone / totalWork);
}
return null;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.