简体   繁体   English

设置其TextProperty绑定后,无法输入JavaFx TextField

[英]Cannot type in JavaFx TextField once its TextProperty is binding is set

To test the property binding in Javafx, I have created two TextField as the following: 为了测试Javafx中的属性绑定,我创建了两个TextField ,如下所示:

public class BindingTest extends Application {

  public void start(Stage stage) throws Exception {

    TextField text1 = new TextField();
    TextField text2 = new TextField();

//      text1.textProperty().bindBidirectional(text2.textProperty());
    text1.textProperty().bind(text2.textProperty());

    VBox root = new VBox(text1, text2);

    stage.setTitle("Binding Test");
    stage.setScene(new Scene(root, 400, 300));
    stage.show();

  }

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

When I use bidirectional binding, I can type in both text fields, and the text property binding works perfectly. 当我使用双向绑定时,我可以在两个文本字段中键入内容,并且文本属性绑定可以完美地工作。 But when I use unidirectional binding, text field 1 does update its content when I type in text field 2, but I cannot type in text field 1 anymore. 但是,当我使用单向绑定时,在输入文本字段2时,文本字段1确实会更新其内容,但是我无法再在文本字段1中输入内容。

Is it normal ? 正常吗?

Yes you need to use a bi-directional binding. 是的,您需要使用双向绑定。

If you use an uni-directional binding you say that the value in text1 has to be the same as the value in text2 which would not be the case if you could type. 如果使用单向绑定,则说明text1中的值必须与text2中的值相同,如果可以键入则不会这样。

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

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