[英]JavaFX how to bind TextProperty to append to TextField (not to set)
[英]Cannot type in JavaFx TextField once its TextProperty is binding is set
为了测试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);
}
}
当我使用双向绑定时,我可以在两个文本字段中键入内容,并且文本属性绑定可以完美地工作。 但是,当我使用单向绑定时,在输入文本字段2时,文本字段1确实会更新其内容,但是我无法再在文本字段1中输入内容。
正常吗?
是的,您需要使用双向绑定。
如果使用单向绑定,则说明text1中的值必须与text2中的值相同,如果可以键入则不会这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.