如何使用javaFx更新TableView Row [英] How to update TableView Row using javaFx

查看:144
本文介绍了如何使用javaFx更新TableView Row的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make some downloads and show the progress inside my table:

这样做我正在使用以下类:

to do that I'm using the following classes:

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和列表我这样做:

and to create the TitledPane, tableview and column tables I'm doing this:

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);
    }

直到这里一切似乎都运行良好,但是当我尝试恢复我的桌子时并更新数据,我的表没有更新。我已经debbuged,一切似乎都工作,即使数据值已更改,但我的表仍然没有更新。查看我的代码:

Until here everything seems to works fine, but when I try to recover my table and update the data, my table is not updated. I've debbuged and everything seems to work, even the data value is changed, but my table still without update. See my code:

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);
        }
    });
}

如果我尝试删除并添加我的行,它不起作用,但是如果我只是用新值添加另一行,我得到一个具有相同值的旧行和一个带有新值的新行。我无法弄清楚我做错了什么,有人可以帮我吗?

If I try to remove and add my row it doesn't work, but if I just add another row with the new values, I got a old row with the same value and a new row with new values. I can't figure out what am I doing wrong, someone can help me??

推荐答案

你不需要添加/删除行以在进度值更改时让表更新。

You shouldn't need to add/remove the row to get the table to update when the progress value changes.

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

The problem is that you're not making the progress property accessible to the TableView. This causes the progress.setCellValueFactory(...) call to wrap your getProgress() value in a new ObservableObjectWrapper. This allows the value to display in the TableView, but it won't notify the table when the value is changed.

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

Add the following to your DownloadDataTable class, and your table will update when the value changes:

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

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

public SimpleStringProperty statusProperty() {
    return this.status;
}

这篇关于如何使用javaFx更新TableView Row的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆