JavaFX 双向绑定不起作用,控件变得不可编辑 [英] JavaFX bidirectional binding not working with the control becoming not editable

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

问题描述

给出一个非常基本的类(导入解析为 javafx 包):

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

public class T07 extends Application implements Initializable{

一些字段代表在 .fxml 文件中定义的控件:

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.

这张装订收据中缺少的成分是什么?

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 种方式 - 输入到 textField 的文本将反映到模型的第一个 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天全站免登陆