Mat*_*ari 1 java javafx javafx-2 javafx-8
我有一个带有复选框的第一列的 TableView。
我设法正确显示复选框并使其可编辑,但是当我单击复选框以选中或取消选中时,复选框会更改但它不会更新我的模型。
我是这样做的:
TableColumn<ContasReceber, Boolean> myCheckBoxColumn = (TableColumn<ContasReceber, Boolean>) tabelaContas.getColumns().get(0);
myCheckBoxColumn.setCellFactory(p -> new CheckBoxTableCell<>());
myCheckBoxColumn.setOnEditCommit(evt -> evt.getRowValue().setChecked(evt.getNewValue()));//It never executes the method setChecked when i click on the checkBox to change it's values.
Run Code Online (Sandbox Code Playgroud)
CheckBoxTableCell
真正设计为映射到BooleanProperty
表模型中的 a (并且通常表最适合此类模型)。在对JavaDoc中CheckBoxTableCell
明确规定,
不会调用通常的编辑回调(例如编辑提交时)
如果您想CheckBox
在模型不使用 a的表格单元格中使用 a BooleanProperty
,最好的办法可能是创建您自己的表格单元格:
myCheckBoxColumn.setCellFactory(p -> {
CheckBox checkBox = new CheckBox();
TableCell<ContasReceber, Boolean> cell = new TableCell<ContasReceber, Boolean>() {
@Override
public void updateItem(Boolean item, boolean empty) {
if (empty) {
setGraphic(null);
} else {
checkBox.setSelected(item);
setGraphic(checkBox);
}
}
};
checkBox.selectedProperty().addListener((obs, wasSelected, isSelected) ->
((ContasReceber)cell.getTableRow().getItem()).setChecked(isSelected));
cell.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
cell.setAlignment(Pos.CENTER);
return cell ;
});
Run Code Online (Sandbox Code Playgroud)