I’m trying to assess the value from ComboBox and based on condition, prevent that change from happening:
<code>ComboBox<String> cb=new ComboBox<>(FXCollections.observableArrayList("One","Two","Three"));
cb.getSelectionModel().selectedItemProperty().addListener((obs,oldVal,newVal)->loadAll(cb,oldVal,newVal));
private void loadAll(ComboBox cb,String oldVal,String newVal){
if(newVal.equals("Two")){
cb.setValue(oldVal);
}
}
</code>
<code>ComboBox<String> cb=new ComboBox<>(FXCollections.observableArrayList("One","Two","Three"));
cb.getSelectionModel().selectedItemProperty().addListener((obs,oldVal,newVal)->loadAll(cb,oldVal,newVal));
private void loadAll(ComboBox cb,String oldVal,String newVal){
if(newVal.equals("Two")){
cb.setValue(oldVal);
}
}
</code>
ComboBox<String> cb=new ComboBox<>(FXCollections.observableArrayList("One","Two","Three"));
cb.getSelectionModel().selectedItemProperty().addListener((obs,oldVal,newVal)->loadAll(cb,oldVal,newVal));
private void loadAll(ComboBox cb,String oldVal,String newVal){
if(newVal.equals("Two")){
cb.setValue(oldVal);
}
}
Problem is, loadAll
will keep calling itself as cb.setValue(oldVal)
is changing the selectedItem again. How to run loadAll
before the change has occurred?