简体   繁体   English

javafx TableCell类中的updateItem()方法

[英]updateItem() method in javafx TableCell class

When is updateItem() method in TableCell called? 何时调用TableCell中的updateItem()方法? Is it when Property associated with that cell changes? 是与该单元格关联的属性发生变化时吗?

In my application I have a thread that downloads content based on hyperlink provided.I have a TableView that displays name and progress of download in two different columns.In the progress column I wanted to have a progressbar and a label at the center of progressbar which displays % downloaded.For that I took help from Progressbar and label in tablecell .But it seems that updateItem() method is not reading the 'progress' variable and -1 is getting read everytime. 在我的应用程序中,我有一个线程根据提供的超链接下载内容。我有一个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 of my Download Class 我的下载类别的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);
    }

In the run method of Download class I am continuously updating 'progress' variable by adding to it the number of downloaded bytes. 在Download类的run方法中,我通过向其添加已下载字节数来不断更新'progress'变量。

There is a progress property in Task , but it is not modified if you write to the progress field you added. Task有一个progress属性,但是如果您写入添加的progress字段,则不会对其进行修改。 ( PropertyValueFactory uses methods to retrieve the result, not fields and a Double field does not provide a way to observe it anyways.) PropertyValueFactory使用方法而不是字段来检索结果,而Double字段始终无法提供观察结果的方法。)

updateProgress should be used to update this property to ensure the property is properly synchronized with the application thread. 应该使用updateProgress来更新此属性,以确保该属性与应用程序线程正确同步。

eg 例如

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