我一直在使用Javafx中的表视图,并且我想动态地控制单击按钮上的单元格,这本身就是表视图的一部分。我试图自己做,但它不应该如此。所以需要帮助,因为他们无法做到。JavaFX:Tableview中按钮点击动态单元格控件?
按钮的TableCell代码:
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);
。请在这件事上给予我帮助。!
>
发帖后我无法检查我的问题,所以我想到了其他一些方式可以为我完成所需的任务,并且它基于您的想法。我没有在这里显示整个表格,但可以理解的是,还会有一个Name列,它在任何情况下都会变成静态的(创建一次,同时呈现)。所以我明确地为它提供Cell Factory,并且每当Name单元格被渲染时设置'setProgress(2);'。因此,我可以使用progress属性的这种行为来交叉检查Progress-Cell-Factory,当它是== 2,'setGraphic(null)',否则'setGraphic(progressBar)'。 – androizer
我会等待一天,让其他用户给出答案,如果可以有任何其他方式,否则我会将你的答案标记为最终答案。 – androizer