JavaFX ComboBox OnChangeListener回滚更改 [英] JavaFX ComboBox OnChangeListener rollback change
问题描述
我要重设 ComboBox
的选择,如下所示:
I'm trying to reset the selection of a ComboBox
like follows:
// private ListView<MyEntityType> f_lItems
f_lItems.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() {
@Override
public void changed(ObservableValue<?> ov, Object t, Object t1) {
if (t1 != null && t1 instanceof MyEntityType) {
MyEntityType pv = (MyEntityType) t1;
// do some condition testing
if (condition) {
// accept
} else
// roll back to previous item
f_lItems.getSelectionModel().select((MyEntityType) t);
}
}
}
});
所以,在尝试将列表重置为旧值后,我得到这个异常:
So, after trying to reset the list to the old value I get this Exception:
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException
at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.subList(Unknown Source)
at javafx.collections.ListChangeListener$Change.getAddedSubList(Unknown Source)
at com.sun.javafx.scene.control.behavior.ListViewBehavior.lambda$new$177(Unknown Source)
at javafx.collections.WeakListChangeListener.onChanged(Unknown Source)
at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(Unknown Source)
看起来我没有得到 List
s / ObservableList
s。
As it seems I don't get the underlying behaviour of List
s / ObservableList
s for this case.
有没有人建议我如何使这项工作?
Does anyone have suggestions how I could make this work?
感谢提前
Adam
Thanks in advance Adam
推荐答案
根据你的意见,你想要的是:当code> ComboBox 更改,检查一个条件,然后如果这个条件不满足,将
ComboBox
的值设置回上一个。
Based on your comment what you want is: When the (selected) value of the ComboBox
is changed, check for a condition and then if this condition is not met, set back the value of the ComboBox
value to the previous one.
为此,您可以使用 valueProperty 与监听器的ComboBox。监听器主体只是检查条件,并且值更新嵌套在 Platform.runLater {...}
块中。
For this you can use for example the valueProperty of the ComboBox with a listener. The listener body is just to check for the condition and the value update is nested in a Platform.runLater{...}
block.
在示例中,它是一个
In the example it is a ComboBox
which can be set only to "Two".
ComboBox<String> cb = new ComboBox<String>(FXCollections.observableArrayList("One", "Two", "Three", "Four"));
cb.valueProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
// If the condition is not met and the new value is not null: "rollback"
if(newValue != null && !newValue.equals("Two")){
Platform.runLater(new Runnable(){
@Override
public void run() {
cb.setValue(oldValue);
}});
}
}
});
...或者您可以使用 selectedItemProperty 也具有相同的结构...
... or you can use the selectedItemProperty also with the same structure ...
cb.getSelectionModel().selectedItemProperty().addListener((obs, oldVal, newVal)->{
if(newVal != null && !newVal.equals("Two")){
Platform.runLater(() -> cb.setValue(oldVal));
}
});
注意:此解决方案不是阻止如标题:回滚已执行的选择。
Note: This solution is not to "prevent" the selection, just as in the title: "roll-back" an already performed selection.
这篇关于JavaFX ComboBox OnChangeListener回滚更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!