因为您需要一个“始终编辑”单元,所以您的实现应该更像
CheckBoxTableCell
ComboBoxTableCell
. 前者绕过了数据库的正常编辑机制
TableView
修改您的
ManufactureTableCell
更像
CheckBoxTableCell
class ManufacturerTableCell extends TableCell<ComputerPart, Manufacturer> {
private final ComboBox<Manufacturer> cboStatus;
private final IntFunction<Property<Manufacturer>> extractor;
private Property<Manufacturer> property;
ManufacturerTableCell(IntFunction<Property<Manufacturer>> extractor, ObservableList<Manufacturer> items) {
this.extractor = extractor;
this.cboStatus = new ComboBox<>();
this.cboStatus.setItems(items);
// removed StringConverter for brevity (accidentally)
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
cboStatus.addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {
if (event.isShortcutDown()) {
getTableView().getSelectionModel().select(getIndex(), getTableColumn());
} else {
getTableView().getSelectionModel().clearAndSelect(getIndex(), getTableColumn());
}
event.consume();
});
}
@Override
protected void updateItem(Manufacturer item, boolean empty) {
super.updateItem(item, empty);
setText(null);
clearProperty();
if (empty) {
setGraphic(null);
} else {
property = extractor.apply(getIndex());
Bindings.bindBidirectional(cboStatus.valueProperty(), property);
setGraphic(cboStatus);
}
}
private void clearProperty() {
setGraphic(null);
if (property != null) {
Bindings.unbindBidirectional(cboStatus.valueProperty(), property);
}
}
}
您可以这样安装它:
// note you could probably share the same ObservableList between all cells
colManufacturer.setCellFactory(param ->
new ManufacturerTableCell(i -> tableView.getItems().get(i).manufacturerProperty(),
FXCollections.observableArrayList(Manufacturer.values())));
如前所述,上述实现绕过了正常的编辑机制;它将价值联系在一起
ComboBox
直接指向模型项的属性。该实现还添加了一个
MOUSE_PRESSED
组合框
根据需要选择行(如果使用单元格选择,则选择单元格)。不幸的是,我不太了解如何在需要时实现选择
是向下的,因此只处理“按”和“快捷方式+按”。
我相信上面的工作方式是您希望的,但我只能使用JavaFX12进行测试。