有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java轻松地将许多JavaFX属性绑定到UINode

我处理的应用程序要求SQL数据库提供不同的值。这里我有一个表,将近40列,我的POJO有JavaFX属性。因此,现在我将这些属性绑定到UI组件,比如textfields等,这些属性由数据库中的数据填充

我现在的做法是手动将所有ui属性绑定到pojo属性。但是这是一个很大的方法,有40个条目可以手动绑定所有属性

你能告诉我一个更好的方法吗?也许像javafx的表系统那样通过字符串绑定属性

@James\u D:谢谢你的回复。我的想法是使用我在fxml中设置的字符串值扩展textfields等,该字符串=要绑定的属性的名称。这几乎就像javafx表的映射系统

@kelopatra:我不想用值填充表格。我已经准备好了这个系统。我想要的是自动化ui和pojo之间的绑定过程。例如:

这是我的pojo,由数据库中的值填充:

public class SimplePojo {

private final StringProperty button_propertie = new SimpleStringProperty();
private final StringProperty lable_propertie = new SimpleStringProperty();

public SimplePojo(String button, String lable){
    this.button_propertie.setValue(button);
    this.lable_propertie.setValue(lable);
}

public String getLable_propertie() {
    return lable_propertie.get();
}

public void setLable_propertie(String value) {
    lable_propertie.set(value);
}

public StringProperty lable_propertieProperty() {
    return lable_propertie;
}

public String getButton_propertie() {
    return button_propertie.get();
}

public void setButton_propertie(String value) {
    button_propertie.set(value);
}

public StringProperty button_propertieProperty() {
    return button_propertie;
}

这是我的控件,用于向用户显示值:

public class FXMLDocumentController implements Initializable {

@FXML
private Label label;
@FXML
private Button button;

@Override
public void initialize(URL url, ResourceBundle rb) {
    //Getting result from Database
    SimplePojo sp = new SimplePojo("label_text", "button_text");
    //Bind properties to ui elements
    doBindings(sp);
}    

public void doBindings(SimplePojo sp){
    this.button.textProperty().bindBidirectional(sp.button_propertieProperty());
    this.label.textProperty().bindBidirectional(sp.lable_propertieProperty());
    //If i have 50 fields in pojo ... this would not end ....
}

控制器中的问题是。。。请看“doBindings”中的评论。。。这个列表几乎不会结束


共 (1) 个答案