JavaFX将整个模型绑定到表单

时间:2016-12-19 17:31:15

标签: javafx binding model

我花了一整天的时间来通过这个JavaFx数据绑定来观察我的模型。 在这一点上,我已经看到,如果我改变这样的一个属性,它就像魅力

selectedTestcase.setFolder("....");

但我如何观察以下内容并刷新我的表单:

if (maybeCase.isPresent()) {
        selectedTestcase = maybeCase.get();
}

所以我改变了完整的模型。我怎么能做到这一点?

型号:

@XmlRootElement(name = "Testcase")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Testcase {

private StringProperty Guid;
@XmlElement(name="GUID")
public String getGuid() {
    return guidProperty().get();
}
public StringProperty guidProperty() {
    if (Guid == null)
        Guid = new SimpleStringProperty(this, "Guid");
    return Guid;
}
public void setGuid(String guid) {
    this.guidProperty().set(guid);
}

private StringProperty caseName;
@XmlElement(name="CaseName")
public String getCaseName() {
    return caseNameProperty().get();
}
public StringProperty caseNameProperty() {
    if (caseName == null)
        caseName = new SimpleStringProperty();
    return caseName;
}
public void setCaseName(String caseName) {
    this.caseNameProperty().set(caseName);
}

视图模型:

public Testcase selectedTestcase = new Testcase();

public void setSelectedTestcase(String folder, String filename) {

    Optional<Testcase> maybeCase = this.AvailableTestCases.stream()
            .filter((t -> t.TestcaseEqualsFolderAndName(folder, filename))).findFirst();

    if (maybeCase.isPresent()) {
        selectedTestcase = maybeCase.get();
    }
}

提前致谢:)

1 个答案:

答案 0 :(得分:0)

感谢James_D和Easybind图书馆:

{{3}}

这似乎是解决方案:

private ObjectProperty<Testcase> testcaseObjectProperty = new SimpleObjectProperty<>(new Testcase());

public MonadicObservableValue<Testcase> selectedTestcase = EasyBind.monadic(testcaseObjectProperty);

public void setSelectedTestcase(String folder, String filename) {

    Optional<Testcase> maybeCase = this.AvailableTestCases.stream()
            .filter((t -> t.TestcaseEqualsFolderAndName(folder, filename))).findFirst();

    if (maybeCase.isPresent()) {
        testcaseObjectProperty.setValue(maybeCase.get());
    }
}

然后是最终的绑定:

txtCaseName.textProperty().bind(viewModel.selectedTestcase.flatMap(Testcase::caseNameProperty).orElse(""));

詹姆斯;你能够解释我这个&#34; orElse&#34;?