选中/取消选中时,TableView上的JavaFx复选框不会更新模型
我有一个表格视图,第一列带有一个复选框 我设法正确显示复选框并使其可编辑,但当我单击复选框以选中或取消选中时,复选框会更改,但不会更新我的模型 我就是这样做的:选中/取消选中时,TableView上的JavaFx复选框不会更新模型,java,javafx,javafx-2,javafx-8,Java,Javafx,Javafx 2,Javafx 8,我有一个表格视图,第一列带有一个复选框 我设法正确显示复选框并使其可编辑,但当我单击复选框以选中或取消选中时,复选框会更改,但不会更新我的模型 我就是这样做的: TableColumn<ContasReceber, Boolean> myCheckBoxColumn = (TableColumn<ContasReceber, Boolean>) tabelaContas.getColumns().get(0); myCheckBoxColumn.setCellFactor
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.
尝试使用以下代码创建列:
checkBoxesCol = new TableColumn<ContasReceber, Boolean>("CheckBox Col");
checkBoxesCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ContasReceber, Boolean>, ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(CellDataFeatures<ContasReceber, Boolean> param) {
return param.getValue().checkedProperty();
}
});
checkBoxesCol.setCellFactory(CheckBoxTableCell.forTableColumn(checkBoxesCol));
值的更新是自动的,无需setOnEditCommit尝试使用以下代码创建列:
checkBoxesCol = new TableColumn<ContasReceber, Boolean>("CheckBox Col");
checkBoxesCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ContasReceber, Boolean>, ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(CellDataFeatures<ContasReceber, Boolean> param) {
return param.getValue().checkedProperty();
}
});
checkBoxesCol.setCellFactory(CheckBoxTableCell.forTableColumn(checkBoxesCol));
值的更新是自动的,不需要setOnEditCommitCheckBoxTableCell实际上是为了映射到表模型中的Boolean属性而设计的,一般来说,表在这种模型中工作得最好。政府明确表示
通常的编辑回调(如编辑提交时)将不会被调用
如果要在模型不使用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 ;
});
CheckBoxTableCell实际上是为了映射到表模型中的BooleanProperty而设计的,通常情况下,表在此类模型中工作得最好。政府明确表示
通常的编辑回调(如编辑提交时)将不会被调用
如果要在模型不使用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 ;
});
我找到了自己的方法,答案很简单: 有一种非常简单的方法可以做到这一点,您不需要使用SimpleBoleAnProperty或其他任何东西修改模型类,只需遵循以下步骤: 1-假设您有一个方法为的Person对象: 2-创建回调类 3-将回调绑定到表列 如果使用FXML,请将回调类放入列中: 不要忘记在FXML中导入类:
<?import org.yourcompany.yourapp.util.PersonUnemployedValueFactory?>
如果没有FXML,请按如下方式执行:
就这样
一切都应该按预期工作,当您单击复选框时,值被设置为backing bean,当您加载表中的项目列表时,复选框值被正确设置。我自己找到了这样做的方法,它非常简单,正如我在中回答的: 有一种非常简单的方法可以做到这一点,您不需要使用SimpleBoleAnProperty或其他任何东西修改模型类,只需遵循以下步骤: 1-假设您有一个方法为的Person对象: 2-创建回调类 3-将回调绑定到表列 如果使用FXML,请将回调类放入列中: 不要忘记在FXML中导入类:
<?import org.yourcompany.yourapp.util.PersonUnemployedValueFactory?>
如果没有FXML,请按如下方式执行:
就这样
一切都应该按预期工作,当您单击复选框时,值被设置为backingbean,当您加载表中的项目列表时,复选框值被正确设置。但是这样我就必须将我的模型与JavaFX代码绑定起来?我不能只使用一个简单的布尔变量而不是SimpleBoleanProperty吗?在上面的代码中,尝试在cellValueFactory回调中使用ReadOnlyBooleanWrapper包装布尔值。上面的方法对我有效,但如果我单击tableview中的复选框,它不会更新值。我做错了什么?我在模型中将它定义为SimpleBoleAnProperty,但这样我就必须将我的模型与JavaFX代码联系起来?我不能只使用一个简单的布尔变量而不是SimpleBoleanProperty吗?在上面的代码中,尝试在cellValueFactory回调中使用ReadOnlyBooleanWrapper包装布尔值。上面的方法对我有效,但如果我单击tableview中的复选框,它不会更新值。我做错了什么?我在模型中将其定义为SimpleBoleAnProperty我尝试了这种方法,但当我单击复选框时,值没有更新回模型类。我尝试了这种方法,但当我单击复选框时,值没有更新回模型类。
TableColumn<Person, CheckBox> column = (TableColumn<Person, CheckBox>) personTable.getColumns().get(0);
column.setCellValueFactory(new PersonUnemployedValueFactory());