[英]How to know if the value of a checkbox in a tableColumn had change in javafx
嗨,我在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"); } });
问题是,当我更改复选框的值时,消息没有出现
从Javadocs中获取CheckBoxTableCell
:
请注意,CheckBoxTableCell呈现“实时”的CheckBox,这意味着CheckBox始终是交互式的,并且可以由用户直接切换。 这意味着该单元不必进入其编辑状态(通常由用户双击该单元)。 这样做的副作用是不会调用通常的编辑回调(例如在编辑提交时)。 如果希望收到有关更改的通知,建议直接观察由CheckBox操纵的布尔属性。
假设您的表模型类Information
具有orientation
属性的属性访问器方法,即
public class Information {
// ...
public BooleanProperty orientationProperty() {
// ...
}
// ...
}
那么当选中和取消选中该复选框时,相关对象的方向属性将自动更新。 因此,您需要做的就是监听这些属性本身的更改:
Information info = new Information(...);
table.getItems().add(info);
info.orientationProperty().addListener((obs, oldValue, newValue)
-> System.out.println("orientation property edited"));
我用这个解决方案:
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()); } } } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.