繁体   English   中英

javafx TableCell类中的updateItem()方法

[英]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.

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