简体   繁体   中英

updateItem() method in javafx TableCell class

When is updateItem() method in TableCell called? 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.

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

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.

There is a progress property in Task , but it is not modified if you write to the progress field you added. ( PropertyValueFactory uses methods to retrieve the result, not fields and a Double field does not provide a way to observe it anyways.)

updateProgress should be used to update this property to ensure the property is properly synchronized with the application thread.

eg

public class Download extends Task<Void>{

    protected Void call() {

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

              ...

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

         return null;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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