我有一个cuttingOperationComboBox
组合框和改变这种组合框的项目和值的方法:JavaFX的属性更改侦听器:空NEWVALUE
public void changeGlass(Glass newGlass)
{
ObservableList<Operation> list = new FilteredList<Operation>(ProductGlassCuttingUI.this.operationsDB.getOperationsList(), operation -> operation.getOperationType().toString().equals("RE") &&
operation.getGlassThickness() == newGlass.getGlassThickness());
if(!list.contains(this.cuttingOperationComboBox.getValue()))
cuttingOperationComboBox.setValue(list.get(0));
cuttingOperationComboBox.setItems(list);
}
我也有变化监听器添加到cuttingOperationComboBox.valueProperty()
。 这是第一次被cuttingOperationComboBox.setValue(list.get(0));
发射,这里一切都很好。但当cuttingOperationComboBox.setItems(list);
引发更改监听器时,虽然list
不为空,但newValue为空。而且它只有在ComboBox可见时才会发生。 Tobe更精确:cuttingOperationComboBox作为TreeCell图形的一部分显示在TreeView中。只要包含组合框的树视图节点被折叠,一切都可以,但是当我展开此节点并显示组合框时,就会出现上述问题。 有人知道我在做什么错?
感谢您的回复。我想要做的只是更改组合框的项目,并保留当前值,如果它是新列表的元素。为了避免上面的问题与空newValue我检查之前采取任何行动在chnage监听器,就像你提出的。现在一切正常,但我只是好奇,为什么newValue有时是空的,而它应该不是。至少我认为是这样,但我可能是错误的,ComboBox类中有些东西我不明白。 – aban
@aban就像我说的,当你调用'setItems()'时,你告诉'ComboBox':“嘿,我给你一个全新的列表。”会发生什么情况是'ComboBox'会假设你原来选择的值不再正确(因为该值甚至可能不会出现在你的新列表中),所以它会清除该值,导致值变为null。 – Jai