button - 将按钮添加到当前正在编辑的行
问题描述
我曾尝试在 google 和 stackoverflow 上搜索我的问题的答案,但我找不到任何答案。
我有一个从数据库和表视图添加/读取数据的程序。我希望能够通过向正在编辑的当前行添加两个按钮(保存和中止)来编辑以前输入的表格视图中的数据。
我在理解回调等例程时遇到了一些麻烦。但是当我开始编辑时,我设法得到一个带有两个按钮的列。但是我在每一列上都有按钮,我只想要当前的按钮
选定的行。另外我真的不明白如何从保存按钮中获取当前对象以便能够保存它。以及中止按钮应该如何工作以取消所有更改。
public void setUpTableView() {
columnAnkomstdatum.setCellValueFactory(new PropertyValueFactory<>("arrivalDate"));
columnSupplier.setCellValueFactory(new PropertyValueFactory<>("supplier"));
columnRadiopharmaceutical.setCellValueFactory(new PropertyValueFactory<>("radiopharmaceutical"));
columnActivity.setCellValueFactory(new PropertyValueFactory<>("startActivity"));
columnCalibrationdate.setCellValueFactory(new PropertyValueFactory<>("startDate"));
columnBatchNumber.setCellValueFactory(new PropertyValueFactory<>("batchNumber"));
columnContaminationControl.setCellValueFactory(new PropertyValueFactory<>("contaminationControll"));
columnRoom.setCellValueFactory(new PropertyValueFactory<>("room"));
columnUser.setCellValueFactory(new PropertyValueFactory<>("user"));
tableview.setEditable(true);
columnSupplier.setEditable(true);
columnSupplier.setCellFactory(ComboBoxTableCell.forTableColumn(supplierList));
columnSupplier.setOnEditCommit(t -> {
ArrayList<Radiopharmaceutical> radioListfromSupplier = new RadiopharmaceuticalDao().getRadiopharmaceuticalsBySupplierName(t.getNewValue().getSupplierName());
radioList = FXCollections.observableArrayList(radioListfromSupplier);
t.getRowValue().setSupplier(t.getNewValue());
columnRadiopharmaceutical.setCellFactory(ComboBoxTableCell.forTableColumn(radioList));
if(tableview.getColumns().size() <= 9) {
addButtonsToTable();
}
});
}
private void addButtonsToTable() {
TableColumn<RegRadio, Void> editRow = new TableColumn<>("Edit");
tableview.getColumns().add(editRow);
Callback<TableColumn<RegRadio, Void>, TableCell<RegRadio, Void>> cellFactory = new Callback<TableColumn<RegRadio,Void>, TableCell<RegRadio,Void>>() {
@Override
public TableCell<RegRadio, Void> call(final TableColumn<RegRadio, Void> param) {
final TableCell<RegRadio, Void> cell = new TableCell<RegRadio, Void>() {
private final Button btnSave = new Button("Save");
private final Button btnAbort = new Button("Avbryt");
{
btnSave.setOnAction((ActionEvent event) -> {
RegRadio rr = getTableView().getItems().get(getIndex());
System.out.println("Saved");
});
}
{
btnAbort.setOnAction((ActionEvent event) -> {
System.out.println("Abort");
});
}
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
HBox pane = new HBox(btnSave, btnAbort);
setGraphic(pane);
}
}
};
return cell;
}
};
editRow.setCellFactory(cellFactory);
tableview.getColumns().add(editRow);
}
解决方案
在您的updateItem
回调中,您可以检查单元格是否在所选行中,以确定是否应该显示按钮。此外,您还需要一个标志来检查用户是否正在编辑。像这样的东西:
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
var selectedCells = tableview.getSelectionModel().getSelectedCells();
if (empty || !isEditing || selectedCells.isEmpty || getTableRow().getIndex() != selectedCells.get(0).getRow()) {
setGraphic(null);
} else {
HBox pane = new HBox(btnSave, btnAbort);
setGraphic(pane);
}
}
另一种方法是根据单元格是否在所选行中来更改按钮的可见性:
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
HBox pane = new HBox(btnSave, btnAbort);
var selectedCells = tableview.getSelectionModel().getSelectedCells();
pane.setVisible(!selectedCells.isEmpty() && getTableRow().getIndex() == selectedCells.get(0).getRow())
setGraphic(pane);
}
}
(我还没有编译和测试这些代码示例)
推荐阅读
- android - RecyclerView 中的布局行为怪异
- r - 格式错误的 JSON 缺少逗号分隔符,在 R 中插入逗号
- java - 将 wicket java 应用程序从 javax 转换为 jakarta
- reactjs - 没有jquery的react js中fullcalendar的标题工具栏上的下拉组件
- python - 在不使用 matplotlib 的情况下创建条形图
- recursion - FSharp 编译器服务:如何确定来自 Typed AST 的模块级声明的相互递归?
- javascript - dateAxis 上的 amchart 最后一个标签不正确可见
- python - AttributeError:“WebDriver”对象没有属性“execute_cdp_cmd”
- ip - 本地网络偶尔会阻止来自设备的传出数据包
- cross-domain - Acrobat 错误:[严格] 忽略语法不正确的策略文件