当前位置: 动力学知识库 > 问答 > 编程问答 >

javafx 8 - How to distinguish changes made by the user from programmatic changes?

问题描述:

I'd like to react to changes made by the user and ignore the ones caused by my code.

For example, I've got a TextField that shows the name of an entry. When the user changes it, I want to save the change back so I've registered a change listener:

nameTextField.textProperty().addListener(

(ObservableValue<? extends String> observable, String oldValue, String newValue) -> {

entry.setName(newValue);

saveEntry();

}

);

The problem is that this change listener is also triggered when I update the UI for new entry:

nameTextField.setText(entry.getName());

I can work around this by using a variable that indicates whether I'd like these changes to be applied, but this feels wrong, it "smells".

Also, this not only about TextFields. I have the same issue with ListViews and ComboBoxes.

Since I'm new to JavaFX and I think that this is a common issue I have a feeling that I'm missing something. So, how is this usually solved in JavaFX?

分享给朋友:
您可能感兴趣的文章:
随机阅读: