CheckBoxTableCell 更改侦听器不起作用 [英] CheckBoxTableCell changelistener not working
问题描述
我正在尝试向我的 CheckBoxTableCells 添加更改侦听器,但它似乎不起作用.我以 CheckBoxes 为例,认为它们的工作方式相同.但是,当我更改其值时没有输出.我如何将一个正确添加到复选框表单元格?
I'm trying to add a change listener to my CheckBoxTableCells but it doesn't seem to be working. I took the example for CheckBoxes figuring they would work the same way. However there is no output when i change its value. How would i add one correctly to a checkboxtablecell?
当前代码:
tc.setCellFactory(new Callback<TableColumn<Trainee, Boolean>, TableCell<Trainee, Boolean>>() {
@Override
public TableCell<Trainee, Boolean> call(TableColumn<Trainee, Boolean> p) {
final CheckBoxTableCell ctCell = new CheckBoxTableCell<>();
ctCell.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue ov, Boolean old_val, Boolean new_val) {
System.out.println(new_val);
}
});
return ctCell;
}
});
推荐答案
selectedProperty
继承自 Cell
只是表示Cell
> 在 UI 组件中被选中.由于您可能没有在 TableView
上启用单元格选择,因此该单元格永远不会被选中.无论如何,这不是您要找的东西;您想知道是否选择了 CheckBox
,而不是 Cell
.
The selectedProperty
is inherited from Cell
and it just indicates whether the Cell
is selected in the UI component. Since you probably don't have cell selection enabled on your TableView
, the cell never becomes selected. This isn't what you're looking for anyway; you want to know whether the CheckBox
is selected, not the Cell
.
这里的技巧是使用 CheckBoxTableCell
的 selectedStateCallback
属性.这是一个将单元格的索引映射到 BooleanProperty
的函数.BooleanProperty
双向绑定到复选框的选中状态.
The trick here is to use the selectedStateCallback
property of the CheckBoxTableCell
. This is a function that maps the index of the cell to a BooleanProperty
. That BooleanProperty
is bound bidirectionally to the selected state of the check box.
如果您的列代表您的 Trainee
类中的实际属性(为了演示,我将其称为 selectedProperty
),那么您可以执行以下操作:>
If your column is representing an actual property in your Trainee
class (I'll just call it selectedProperty
for demonstration) then you can do something like this:
final CheckBoxTableCell<Trainee, Boolean> ctCell = new CheckBoxTableCell<>();
ctCell.setSelectedStateCallback(new Callback<Integer, ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(Integer index) {
return table.getItems().get(index).selectedProperty();
}
});
然后将Trainee
类中的属性与复选框状态双向绑定.如果您需要做的不仅仅是在选中/取消选中复选框时更新模型对象,您只需观察该属性即可.
Then the property in the Trainee
class with be bidirectionally bound to the check box state. If you need to do more than just update your model object when the check box is selected/deselected, you can just observe that property.
如果Trainee
类中没有属性,你可以创建一个BooleanProperty
并观察它:
If you don't have a property in the Trainee
class, you can just create a BooleanProperty
and observe it:
final CheckBoxTableCell<Trainee, Boolean> ctCell = new CheckBoxTableCell<>();
final BooleanProperty selected = new SimpleBooleanProperty();
ctCell.setSelectedStateCallback(new Callback<Integer, ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(Integer index) {
return selected ;
}
});
selected.addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> obs, Boolean wasSelected, Boolean isSelected) {
System.out.println(isSelected);
}
});
像往常一样,所有这些代码在 Java 8 中看起来好多了.
As usual, all this code looks a lot nicer in Java 8.
这篇关于CheckBoxTableCell 更改侦听器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!