简体   繁体   English

不能与控件一起使用的JavaFX双向绑定变得不可编辑

[英]JavaFX bidirectional binding not working with the control becoming not editable

Give a very elementary class (imports resolved into javafx packages): 给一个非常基础的类(导入解析为javafx包):

public class T07 extends Application implements Initializable{

with some fields representing controls defined in an .fxml file: 一些字段表示.fxml文件中定义的控件:

@FXML TextField text01;

and a data model that uses Property wrappers in the most basic way: 以及以最基本的方式使用Property包装器的数据模型:

public static class DataModel {

    StringProperty first = new SimpleStringProperty();
    //getter
    public String getFirst() {return first.get();}
    //setter
    public void setFirst(String first) {this.first.set(first);}
    //new "property" accessor
    public StringProperty firstProperty() {return first;}

}

I try to bind the ui control with the data model inside the initialize: 我尝试将ui控件与初始化中的数据模型绑定:

@Override
public void initialize(URL arg0, ResourceBundle arg1) {

   Bindings.bindBidirectional(text01.textProperty(), dm.firstProperty());

}

but doing so, i get a non-editable control . 但这样做, 我得到了一个不可编辑的控件 commenting out the Bindings.bindBidirectional line, the control becomes normally editable and its value accessible through text01 field. 注释掉Bindings.bindBidirectional行,该控件通常可编辑,其值可通过text01字段访问。

What is the missing ingredient in this binding receipe? 这个绑定食谱中缺少的成分是什么?

Bidirectional binding example: 双向绑定示例:

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class JavaFXApplication10 extends Application {

    private Model model = new Model();

    @Override
    public void start(Stage primaryStage) {

        final TextField textField = new TextField();

        Bindings.bindBidirectional(textField.textProperty(), model.firstProperty());

        // Track the changes
        model.firstProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
                System.out.println("model old val: " + arg1);
                System.out.println("model new val: " + arg2);
                System.out.println();
            }
        });

        textField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
                System.out.println("textField old val: " + arg1);
                System.out.println("textField new val: " + arg2);
                System.out.println();
            }
        });

        Button btn = new Button();
        btn.setText("Change the model's text");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                model.setFirst("changed from button action");
                System.out.println("Done.");
            }
        });

        BorderPane root = new BorderPane();
        root.setTop(textField);
        root.setBottom(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    // Data Model
    public static class Model {

        StringProperty first = new SimpleStringProperty();
        //getter

        public String getFirst() {
            return first.get();
        }
        //setter

        public void setFirst(String first) {
            this.first.set(first);
        }
        //new "property" accessor

        public StringProperty firstProperty() {
            return first;
        }
    }
}

Bidirectional binding: 双向绑定:
1 way - Text entered into textField will be reflected to the model's first stringProperty. 1路 - 输入textField的文本将反映到模型的第一个stringProperty中。
2nd contrary way - By clicking the button, you will notice that when model's first stringProperty is set, the textfield's text value will be also changed. 第二种相反方式 - 通过单击按钮,您会注意到当设置模型的第一个stringProperty时,文本字段的文本值也将更改。

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

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