4

我正在尝试进行一些下载并在我的表格中显示进度:

在此处输入图像描述

为此,我正在使用以下类:

public class DownloadDataTable {


    private SimpleDoubleProperty progress;
    private SimpleStringProperty type;
    private SimpleStringProperty status;

    public DownloadDataTable(double progress, String type, String status) {
        this.progress = new SimpleDoubleProperty(progress);
        this.type = new SimpleStringProperty(type);
        this.status = new SimpleStringProperty(status);
    }

    public double getProgress() {
        return progress.get();
    }

    public void setProgress(double progress) {
        this.progress.set(progress);
    }

    public String getType() {
        String retorno;
        if (type==null){
            retorno="";
        }else{
            retorno=type.get();
        }
        return retorno;

    }

    public void setType (String type) {
        this.type.set(type);
    }

    public String getStatus(){
        String retorno;
        if (status==null){
            retorno="";
        } else{
            retorno=status.get();
        }
        return retorno;
    }

    public void setStatus(String status){
        this.status.set(status);
    }
}

并创建 TitledPane、tableview 和列表,我正在这样做:

public void addDownloadToTitledPane(DownloadContent downloadContent) {

        MetaDados metaDado = downloadContent.getMetaDado();

        String title = metaDado.getName();

        if (title.length() > 60) {

            title = title.substring(0, 57) + "...";
        }
        TableView downloadTable = new TableView();

        TableColumn<DownloadDataTable, Double> progress = new TableColumn<>("progress");
        progress.setCellFactory(new Callback<TableColumn<DownloadDataTable, Double>, TableCell<DownloadDataTable, Double>>() {
            @Override
            public TableCell<DownloadDataTable, Double> call(TableColumn<DownloadDataTable, Double> p) {

                final ProgressBar progressBar = new ProgressBar(-1);

                final TableCell cell = new TableCell<DownloadDataTable, Double>() {
                    @Override
                    protected void updateItem(Double t, boolean bln) {
                        super.updateItem(t, bln);
                        if (bln) {
                            setText(null);
                            setGraphic(null);
                        } else {
                            progressBar.setProgress(t);
                            progressBar.prefWidthProperty().bind(this.widthProperty());
                            setGraphic(progressBar);
                            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                        }
                    }
                };


                cell.setAlignment(Pos.CENTER);
                return cell;
            }
        });
        progress.setCellValueFactory(new PropertyValueFactory<DownloadDataTable, Double>("progress"));
        progress.setText("Progresso");

        TableColumn<DownloadDataTable, String> type = new TableColumn<>("type");
        type.setCellFactory(new Callback<TableColumn<DownloadDataTable, String>, TableCell<DownloadDataTable, String>>() {
            @Override
            public TableCell<DownloadDataTable, String> call(TableColumn<DownloadDataTable, String> p) {
                TableCell cell = new TableCell<DownloadDataTable, String>() {
                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        setText(empty ? null : getString());
                        setGraphic(null);
                    }

                    private String getString() {
                        return getItem() == null ? "" : getItem().toString();
                    }
                };

                cell.setAlignment(Pos.CENTER);
                return cell;
            }
        });
        type.setCellValueFactory(new PropertyValueFactory<DownloadDataTable, String>("type"));
        type.setText("Tipo");

        TableColumn<DownloadDataTable, String> status = new TableColumn<>("status");
        status.setCellFactory(new Callback<TableColumn<DownloadDataTable, String>, TableCell<DownloadDataTable, String>>() {
            @Override
            public TableCell<DownloadDataTable, String> call(TableColumn<DownloadDataTable, String> p) {
                TableCell cell = new TableCell<DownloadDataTable, String>() {
                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        setText(empty ? null : getString());
                        setGraphic(null);
                    }

                    private String getString() {
                        return getItem() == null ? "" : getItem().toString();
                    }
                };

                cell.setAlignment(Pos.CENTER);
                return cell;
            }
        });

        status.setCellValueFactory(new PropertyValueFactory<DownloadDataTable, String>("status"));
        status.setText("Status");

        downloadTable.getColumns().addAll(progress, type, status);

        List<PendingComponents> pendingComponents = downloadContent.getPendingComponents();

        ObservableList<DownloadDataTable> data = FXCollections.observableArrayList();

        for (PendingComponents pendingComponent : pendingComponents) {

            String typeComponent;

            if (pendingComponent.getType().equalsIgnoreCase(Constants.HTML)) {
                typeComponent = "Conteúdo Principal";
            } else {
                typeComponent = "Pacote de Imagens";
            }
            data.add(new DownloadDataTable(-1, typeComponent, "Preparando download"));
        }

        downloadTable.setItems(data);
        downloadTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        TitledPane downloadPane = new TitledPane(title, downloadTable);

        downloadPane.setId(metaDado.getOfflineUuid());
        vBoxDownload.getChildren().add(downloadPane);
    }

直到这里一切似乎都正常,但是当我尝试恢复我的表并更新数据时,我的表没有更新。我已经调试过,一切似乎都正常,即使数据值发生了变化,但我的表仍然没有更新。查看我的代码:

private void publishProgress(final String msg) {
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            TitledPane titledPane = (TitledPane) controller.vBoxDownload.lookup("#"+metaDado.getOfflineUuid());
            TableView table = (TableView) titledPane.getContent();

            DownloadDataTable data = (DownloadDataTable) table.getItems().get(0);
            data.setProgress(100);
            data.setStatus(msg);
        }
    });
}

如果我尝试删除并添加我的行它不起作用,但如果我只是添加具有新值的另一行,我会得到一个具有相同值的旧行和一个具有新值的新行。我不知道我在做什么错,有人可以帮助我吗?

4

1 回答 1

8

当进度值更改时,您不需要添加/删除行来更新表。

问题是您没有使 TableView 可以访问进度属性。这会导致progress.setCellValueFactory(...)调用将您的 getProgress() 值包装在新的 ObservableObjectWrapper 中。这允许值显示在 TableView 中,但不会在值更改时通知表。

将以下内容添加到您的 DownloadDataTable 类中,您的表将在值更改时更新:

public SimpleDoubleProperty progressProperty() {
    return this.progress;
}

public SimpleStringProperty typeProperty() {
    return this.type;
}

public SimpleStringProperty statusProperty() {
    return this.status;
}
于 2013-07-31T02:47:25.993 回答