如何知道tableColumn中复选框的值是否在javafx中发生了变化 [英] How to know if the value of a checkbox in a tableColumn had change in javafx
问题描述
您好我在tablecolumn中创建了一个复选框:
hi I created a checkbox in 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");
}
});
问题是当我更改复选框的值时,消息没有出现
The problem is when I changed the value of the checkbox the message didn’t appear
推荐答案
来自 CheckBoxTableCell
:
请注意,CheckBoxTableCell呈现CheckBox'live',意思是
CheckBox始终是交互式的,并且可以通过
直接切换到用户。这意味着单元格不必进入
编辑状态(通常是用户双击单元格)。
的副作用是不会调用通常的编辑回调(例如
编辑提交)。如果您希望收到
更改的通知,建议直接观察由CheckBox操纵的布尔属性
。
Note that the CheckBoxTableCell renders the CheckBox 'live', meaning that the CheckBox is always interactive and can be directly toggled by the user. This means that it is not necessary that the cell enter its editing state (usually by the user double-clicking on the cell). A side-effect of this is that the usual editing callbacks (such as on edit commit) will not be called. If you want to be notified of changes, it is recommended to directly observe the boolean properties that are manipulated by the CheckBox.
假设您的表模型类信息
具有 orientation
属性的属性访问器方法,即
Assuming your table model class Information
has a property accessor method for the orientation
property, i.e.
public class Information {
// ...
public BooleanProperty orientationProperty() {
// ...
}
// ...
}
然后,当选中和取消选中复选框时,相关对象的orientation属性将自动更新。因此,您需要做的就是自己监听这些属性的变化:
then the orientation property of the relevant object will be updated automatically when the check box is selected and de-selected. Hence all you need to do is listen for changes on those properties themselves:
Information info = new Information(...);
table.getItems().add(info);
info.orientationProperty().addListener((obs, oldValue, newValue)
-> System.out.println("orientation property edited"));
这篇关于如何知道tableColumn中复选框的值是否在javafx中发生了变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!