JavaFX:在 Tableview 中单击按钮时的动态单元格控件?

JavaFX : Dynamic cell control on button click in Tableview?

我一直在使用 Javafx 中的 Table 视图,我想在单击按钮时动态控制单元格,该按钮本身也是 Table 视图的一部分。我试着自己做,但它没有按预期工作。所以需要帮助,因为他们做不到。

按钮Table单元格代码:

 dlColumn.setCellValueFactory(new PropertyValueFactory<>(""));
 dlColumn.setCellFactory(new Callback<TableColumn<SharedFileInfo, String>, TableCell<SharedFileInfo, String>>() {

        @Override
        public TableCell<SharedFileInfo, String> call(TableColumn<SharedFileInfo, String> param) {

            final TableCell<SharedFileInfo, String> cell;
            cell = new TableCell<SharedFileInfo, String>() {
                final Button downloadButton = new Button();

                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty) {
                        setGraphic(null);
                        setText(null);
                    } else {
                        downloadButton.setOnAction((ActionEvent ae) -> {
                             progressColumn.setCellValueFactory(new PropertyValueFactory<>("progress"));
                             progressColumn.setCellFactory(new Callback<TableColumn<SharedFileInfo, Double>, TableCell<SharedFileInfo, Double>>() {

                        @Override
                        public TableCell<SharedFileInfo, Double> call(TableColumn<SharedFileInfo, Double> param) {

                        final ProgressBar progressBar = new ProgressBar();

                        final TableCell<SharedFileInfo, Double> cell;
                        cell = new TableCell<SharedFileInfo, Double>() {

                              @Override
                              protected void updateItem(Double item, boolean empty) {
                              super.updateItem(item, true);
                              if (empty) {
                                 setGraphic(null);
                                 setText(null);
                              } else {
                                  SharedFileInfo sharedinfo = getTableView().getItems().get(this.getIndex());
                                  progressBar.progressProperty().bind(sharedinfo.getProgress());

                                  setGraphic(progressBar);
                                  progressBar.prefWidthProperty().bind(this.widthProperty());
                                  setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                                   }
                              }

                           };
                           return cell;
                       }
                   });
                        });
                        setGraphic(downloadButton);
                        setText(null);
                    }
                }
            };
            return cell;
        }
    });


在单击按钮之前,一切似乎都按预期工作,但是当我单击按钮时,整个 progress 列被空 Progress Bars 填充,这是我不知道的不想。我想要,每当用户单击按钮时,只有该行显示 Progress Bar,所有其余设置为 setGraphic(null);。请帮我解决这个问题!
>

目前我无法访问我的IDE,所以我会尝试写一些纯文本。

单击(任何)下载按钮,您将一个新的 CellFactory 分配给整个列,这不会区分您想要成为 "progressed" 的行和所有其他行。 您的所有记录都引用了它们的进度属性,但只有您单击的 SharedFileInfo-Row 的一个进度属性 发生了变化。

如果你想继续这样做,在每个 ButtonClick 上添加一个 CellFactory,
(我不建议这样做,因为你也可以设置一次并实现一个触发器 Boolean属性 例如) 您可以检查单击的按钮行的 SharedFileInfo 实例是否 == 内部 CellFactory

中的 SharedFileInfo 实例

提示:Javafx, get the object referenced by a TableCell

编辑: 如何实现您的目标的示例如下:

  1. 创建全局 SharedFileInfo 字段
  2. 在开头设置CellFactories
  3. Button-Column-Factory:在 Button-Click 上设置全局字段 = clickedCells SharedFileInfo-instance
  4. Progress-Column-Factory:每个单元格检查其 SharedFileInfo 实例是否 == 您的全局字段的值,如果是,则显示其进度条

请注意,在设置全局字段的值后,您可能必须 "tell" 您的单元格才能更新它们的外观。我建议使用全局 属性+ChangeListener.