java JavaFX 双向绑定不起作用,控件变得不可编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11314863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX bidirectional binding not working with the control becoming not editable
提问by AgostinoX
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:
以及以最基本的方式使用属性包装器的数据模型:
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?
这个装订收据中缺少什么成分?
回答by Uluk Biy
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.
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.
双向绑定:
1 种方式 - 输入到 textField 的文本将反映到模型的第一个 stringProperty。
第二种相反的方式 - 通过单击按钮,您会注意到当模型的第一个 stringProperty 设置时,文本字段的文本值也将更改。