我试图从'Information'类中获取'Active'值并将其设置为TableView Column作为Checkbox,因此用户可以编辑。我跟随我的控制器:
activeColumn.setCellValueFactory(new PropertyValueFactory<Information, Boolean>("Active"));
final Callback<TableColumn<Information, Boolean>, TableCell<Information, Boolean>> cellFactory = CheckBoxTableCell.forTableColumn(activeColumn);
activeColumn.setCellFactory(new Callback<TableColumn<Information, Boolean>, TableCell<Information, Boolean>>() {
@Override
public TableCell<Information, Boolean> call(TableColumn<Information, Boolean> column) {
TableCell<Information, Boolean> cell = cellFactory.call(column);
cell.setAlignment(Pos.CENTER);
return cell ;
}
});
activeColumn.setCellFactory(cellFactory);
activeColumn.setEditable(true);
这是我的'信息'类,我将Active值设为true / false
public Information(Hashtable information) {
:::
String strActive = cvtStr(information.get("Active"));
if (strActive.equals("1"))
this.active = true;
else if (strActive.equals("0"))
this.active = false;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
当我运行它时,我没有检查复选框,其中'Active'为真。这是截图:
有人会告诉我我哪里做错了吗?或者还有其他方法可以完成这项工作吗?
非常感谢任何帮助
答案 0 :(得分:1)
在模型类中使用JavaFX属性:
public class Information {
private final BooleanProperty active = new SimpleBooleanProperty();
public Information(Hashtable information) {
// ...
String strActive = cvtStr(information.get("Active"));
if (strActive.equals("1"))
setActive(true);
else if (strActive.equals("0"))
setActive(false);
}
public BooleanProperty activeProperty() {
return active ;
}
public final boolean isActive() {
return activeProperty().get();
}
public final void setActive(boolean active) {
activeProperty().set(active);
}
// ...
}
请注意,您的单元格值工厂出现错误(您莫名其妙地设置了两次):它应该是
activeColumn.setCellValueFactory(new PropertyValueFactory<Information, Boolean>("active"));
更好的是
activeColumn.setCellValueFactory(cellData -> cellData.getValue().activeProperty());
答案 1 :(得分:-1)
我发现将数据对象的属性设为 CheckBox 类型而不是 boolean 类型要简单得多。然后在 tableview 中使用属性更容易和更直接。