JavaFX的双向绑定未在控制变得不可编辑工作 [英] JavaFX bidirectional binding not working with the control becoming not editable

查看:838
本文介绍了JavaFX的双向绑定未在控制变得不可编辑工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

举个很基本类(进口解析到JavaFX包):

Give a very elementary class (imports resolved into javafx packages):

public class T07 extends Application implements Initializable{

某些领域重新在.fxml文件中定义的presenting控制:

with some fields representing controls defined in an .fxml file:

@FXML TextField text01;

和使用属性包装中最基本的方式一个数据模型:

and a data model that uses Property wrappers in the most basic way:

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;}

}

我尝试的UI控件与初始化里面的数据模型绑定:

I try to bind the ui control with the data model inside the initialize:

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

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

}

但这样做,我得到一个不可编辑的控制。注释掉Bindings.bindBidirectional线,控制变得通常可编辑的,其价值通过访问现场text01

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.

什么是这种结合receipe缺少的成分?

What is the missing ingredient in this binding receipe?

推荐答案

双向绑定例如:

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;
        }
    }
}

双向绑定:结果
1路 - 文本输入到文本框将反映到模型的第一stringProperty结果。
第二相反的方式 - 通过点击该按钮,你会发现,当模特的第一stringProperty设置,文本字段的文本值也将改变。

Bidirectional binding:
1 way - Text entered into textField will be reflected to the model's first 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.

这篇关于JavaFX的双向绑定未在控制变得不可编辑工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆