简体   繁体   English

JavaFX:双向绑定不更新 TextField

[英]JavaFX: Bidirectional Binding does not update TextField

I'm playing around with JavaFX and try to create a GUI which saves its state right after a change.我正在玩 JavaFX 并尝试创建一个 GUI,它在更改后立即保存其状态。

Class UiState encapsulates all properties:类 UiState 封装了所有属性:

public class UiState {

  private static final Preferences PREFS = Preferences.userNodeForPackage(UiState.class);
  public static final StringProperty FOLDER_PROP_STATISTICS = new BindingProperty("FOLDER_PROP_STATISTICS",
        "C:\\Temp\\");

    //This is a Stringproperty, which accesses the Preferences 
    protected static class BindingProperty extends SimpleStringProperty {
        private final String def;
        private final String bindingName;

        public BindingProperty(final String bindingName) {
            this(bindingName, "");
        }

        public BindingProperty(final String bindingName, final String def) {
            this.def = def;
            this.bindingName = bindingName;
        }

        @Override
        public String get() {
            return PREFS.get(bindingName, def);
        }

        @Override
        public void set(final String newValue) {
            PREFS.put(bindingName, newValue);

        }

    }

The GUI is created like this: GUI 是这样创建的:

protected void configFileComponents() {

    final TextField folderTextField = getPropertyTextFieldWithLabel("Target folder", UiState.FOLDER_PROP_STATISTICS );

    folderTextField.setOnMouseClicked(target -> {
        onFileChooseButtonClicked();

    });
}

private void onFileChooseButtonClicked() {
    final DirectoryChooser chooser = new DirectoryChooser();
    chooser.setTitle("Select target folder");
    final String folder = getFolderProperty().get();
    if (!(folder == null || folder.isEmpty())) {
        chooser.setInitialDirectory(new File(folder));
    }

    final File result = chooser.showDialog(getScene().getWindow());
    if (result != null) {
        getFolderProperty().set(result.getAbsolutePath());
    }
}

protected TextField getPropertyTextFieldWithLabel(final String label, final StringProperty property) {
    final Label fileLabel = new Label(label);
    addLeft(fileLabel);
    final TextField result = new TextField();
    result.setText(property.get());
    addRight(result);
    //I thought this would be enough
    result.textProperty().bindBidirectional(property);
    return result;
}

Once a folder is selected via DirectoryChooser , the StringProperty FOLDER_PROP_STATISTICS is updated.通过DirectoryChooser选择文件夹后,将更新 StringProperty FOLDER_PROP_STATISTICS But the TextField does not display the new value.但 TextField 不显示新值。 Did I miss something, or did I make a fundamental mistake?我错过了什么,还是我犯了一个根本性的错误?

VGR was right, it was naiv to override get() and set() BindingProperty . VGR 是对的,覆盖 get() 和 set() BindingProperty My new Class looks like this and works like a charm:我的新班级看起来像这样,工作起来很有魅力:

protected static class BindingProperty extends SimpleStringProperty {

    public BindingProperty(final String bindingName, final String def) {
        super(PREFS.get(bindingName, def));
        this.addListener(
                (ChangeListener<String>) (observable, oldValue, newValue) -> PREFS.put(bindingName, newValue));
    }

    public BindingProperty(final String bindingName) {
        this(bindingName, "");
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM