您好我在tablecolumn中创建了一个复选框:
col_orien.setCellValueFactory(new PropertyValueFactory<Information, Boolean>("orientation"));
col_orien.setCellFactory(CheckBoxTableCell.forTableColumn(col_orien));
col_orien.setEditable(true);
col_orien.setOnEditCommit(new EventHandler<CellEditEvent<Information,Boolean>>() {
@Override
public void handle(CellEditEvent<Information, Boolean> event) {
System.out.println("Edit commit");
}
});
问题是当我更改复选框的值时,消息没有出现
答案 0 :(得分:1)
来自Javadocs for CheckBoxTableCell
:
请注意,CheckBoxTableCell呈现CheckBox&#39; live&#39;,意思是 CheckBox始终是交互式的,可以直接切换 用户。这意味着细胞不必进入其中 编辑状态(通常由用户双击单元格)。一个 副作用是通常的编辑回调(例如on 编辑提交)将不会被调用。如果你想得到通知 更改时,建议直接观察布尔属性 由CheckBox操纵。
假设您的表模型类Information
具有orientation
属性的属性访问器方法,即
public class Information {
// ...
public BooleanProperty orientationProperty() {
// ...
}
// ...
}
然后,当选中和取消选中复选框时,相关对象的orientation属性将自动更新。因此,您需要做的就是自己监听这些属性的变化:
Information info = new Information(...);
table.getItems().add(info);
info.orientationProperty().addListener((obs, oldValue, newValue)
-> System.out.println("orientation property edited"));
答案 1 :(得分:0)
我使用这个解决方案:
ObservableList<Information> data = FXCollections.<Information>observableArrayList(
new Callback<Information, Observable[]>() {
@Override
public Observable[] call(Information inf) {
return new Observable[]{inf.orientationProperty(),inf.istProperty()};
}
}
);
data.addListener(new ListChangeListener<Information>() {
@Override
public void onChanged(
javafx.collections.ListChangeListener.Change<? extends Information> change) {
System.out.println("List changed");
while (change.next()) {
if (change.wasUpdated()) {
System.out.println("List updated");
System.out.println(change.getAddedSubList());
}
}
}
});
&#13;