使用EasyBind

时间:2017-08-03 11:55:29

标签: java javafx javafx-bindings

我有一个包含ModelItem项目的主/详细面板。每个ModelItem都有一个ListProperty<ModelItemDetail>,每个ModelItemDetail都有一些StringProperty

在详细信息面板中,我希望显示一个Label,其文字的边界将与当前所选ModelItemDetail的每个ModelItem的属性相关联。最终值可能取决于其他外部属性,例如选择了“详细信息”面板上的CheckBox(即,如果选中该复选框,则bProperty的值不包括在结果中)

此绑定使用Bindings.createStringBinding()完成了我想要的内容:

ObservableValue<ModelItem> selectedItemBinding = EasyBind.monadic(mdModel.selectedItemProperty()).orElse(new ModelItem());

// API Label Binding
apiLabel.textProperty().bind(Bindings.createStringBinding( 
    () -> selectedItemBinding.getValue().getDetails().stream()
        .map(i -> derivedBinding(i.aProperty(), i.bProperty()))
        .map(v->v.getValue())
        .collect(Collectors.joining(", "))
    , mdModel.selectedItemProperty(), checkBox.selectedProperty()));

例如:

private ObservableValue<String> derivedBinding(ObservableValue<String> aProp, ObservableValue<String> bProp) {
    return EasyBind.combine(aProp, bProp, checkBox.selectedProperty(), 
            (a, b, s) -> Boolean.TRUE.equals(s) ? new String(a + " <" + b + ">") : a);
}

我最近发现了EasyBind,我试图用它替换一些API Bindings。我找不到用EasyBind表达这种绑定的方法。显然,我的代码的主要问题是因为s​​electedItem是一个属性,我不能将其详细信息用作ObservableList,我必须坚持使用ObservableValue<ObservableList>>。通过EasyBind.map(ObservableList)EasyBind.combine(ObservableList)链接转换很不方便,这似乎是实现此绑定的理想候选者。在某些时候,我想过创建一个本地ListProperty并通过selectedItem上的一个监听器将它绑定到selectedItem的细节,但它看起来太冗长和不洁净。

我尝试过像这样强制使用EasyBind API:

ObservableValue<ObservableList<ModelItemDetail>> ebDetailList = EasyBind.select(selectedItemBinding).selectObject(ModelItem::detailsProperty);
MonadicObservableValue<ObservableList<ObservableValue<String>>> ebDerivedList = EasyBind.monadic(ebDetailList).map(x->EasyBind.map(x, i -> derivedBinding(i.aProperty(), i.bProperty())));
MonadicObservableValue<ObservableValue<String>> ebDerivedValueBinding = ebDerivedList.map(x->EasyBind.combine(x, stream -> stream.collect(Collectors.joining(", "))));
easyBindLabel.textProperty().bind(ebDerivedValueBinding.getOrElse(new ReadOnlyStringWrapper("Nothing to see here, move on")));

但我感觉最后getOrElse只是在初始化时被调用,并且在selectedItem更改时不会更新。

我也尝试过立即获取ObservableList,但不能指望其他任何空白列表:

ObservableList<ModelItemDetail> ebDetailList = EasyBind.select(selectedItemBinding).selectObject(ModelItem::detailsProperty).get();
ObservableList<ObservableValue<String>> ebDerivedList = EasyBind.map(ebDetailList, i -> derivedBinding(i.aProperty(), i.bProperty()));
ObservableValue<String> ebDerivedValueBinding = EasyBind.combine(ebDerivedList, stream -> stream.collect(Collectors.joining(", "))).orElse("Nothing to see here, move on");
easyBindLabel2.textProperty().bind(ebDerivedValueBinding);

我甚至尝试使用EasyBind.subscribe来监听selectedItem更改并重新绑定(对此不太确定但我不认为需要重新绑定,所有内容都可以执行计算):

EasyBind.subscribe(selectedItemBinding, newValue -> {
    if (newValue != null) {
            ObservableList<ObservableValue<String>> l = 
                EasyBind.map(newValue.getDetails(), 
                             i -> derivedBinding(i.aProperty(), i.bProperty()));
            easyBindLabelSub.textProperty().bind(
                    EasyBind.combine(l, 
                            strm -> strm.collect(Collectors.joining(", "))
                    ));}});

这部分有效,实际上它正在收听复选框更改,但奇怪的是只有第一次更改。我不知道为什么(会很了解)。 如果我添加另一个 EasyBind.Subscribe来订阅checkbox.selectedProperty,它会按预期工作,但这也太冗长和不干净了。如果我自己将一个API监听器添加到selectedItemProperty并在那里执行绑定,也会发生同样的情况。

我使用EasyBind来表达这种绑定的动机正是摆脱了明确表达绑定依赖关系的需要,并试图进一步简化它。我提出的所有方法都明显比API更糟糕,因为我对它并不完全满意。

我仍然对JavaFX很陌生,我试图绕过这个问题。我想了解发生了什么,并找出是否有简洁而优雅的方式来表达与EasyBind的这个绑定。我开始怀疑EasyBind是否还没有为这个用例做好准备(顺便提一下,我认为这种情况很少见)。不过,可能我错过了一些微不足道的东西。

这是一个MVCE,展示了我尝试过的一些方法,以及API绑定按预期工作:

package mcve.javafx;

import java.util.*;
import java.util.stream.*;

import javafx.application.*;
import javafx.beans.binding.*;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

import org.fxmisc.easybind.*;
import org.fxmisc.easybind.monadic.*;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    private CheckBox checkShowMore;

    @Override
    public void start(Stage primaryStage) {
        try {
            // Initialize model
            MasterDetailModel mdModel = new MasterDetailModel();
            ObservableList<ModelItem> itemsList = FXCollections.observableArrayList();
            for (int i=0;i<5;i++) { itemsList.add(newModelItem(i)); }

            // Master
            ListView<ModelItem> listView = new ListView<ModelItem>();
            listView.setItems(itemsList);
            listView.setPrefHeight(150);
            mdModel.selectedItemProperty().bind(listView.getSelectionModel().selectedItemProperty());

            //Detail
            checkShowMore = new CheckBox();
            checkShowMore.setText("Show more details");
            VBox detailVBox = new VBox();           
            Label apiLabel = new Label();
            Label easyBindLabel = new Label();
            Label easyBindLabel2 = new Label();
            Label easyBindLabelSub = new Label();
            Label easyBindLabelLis = new Label();
            detailVBox.getChildren().addAll(
                    checkShowMore, 
                    new TitledPane("API Binding", apiLabel), 
                    new TitledPane("EasyBind Binding", easyBindLabel),
                    new TitledPane("EasyBind Binding 2", easyBindLabel2),
                    new TitledPane("EasyBind Subscribe", easyBindLabelSub),
                    new TitledPane("Listener+EasyBind Approach", easyBindLabelLis)
            );

            // Scene
            Scene scene = new Scene(new VBox(listView, detailVBox),400,400);
            primaryStage.setScene(scene);
            primaryStage.setTitle("JavaFX/EasyBind MVCE");

            // --------------------------
            // -------- BINDINGS --------
            // --------------------------
            ObservableValue<ModelItem> selectedItemBinding = EasyBind.monadic(mdModel.selectedItemProperty()).orElse(new ModelItem());

            // API Label Binding
            apiLabel.textProperty().bind(Bindings.createStringBinding( 
                () -> selectedItemBinding.getValue().getDetails().stream()
                    .map(i -> derivedBinding(i.aProperty(), i.bProperty()))
                    .map(v->v.getValue())
                    .collect(Collectors.joining(", "))
                , mdModel.selectedItemProperty(), checkShowMore.selectedProperty()));

            // EasyBind Binding Approach 1
            {
            ObservableValue<ObservableList<ModelItemDetail>> ebDetailList = EasyBind.select(selectedItemBinding).selectObject(ModelItem::detailsProperty);
            MonadicObservableValue<ObservableList<ObservableValue<String>>> ebDerivedList = EasyBind.monadic(ebDetailList).map(x->EasyBind.map(x, i -> derivedBinding(i.aProperty(), i.bProperty())));
            MonadicObservableValue<ObservableValue<String>> ebDerivedValueBinding = ebDerivedList.map(x->EasyBind.combine(x, stream -> stream.collect(Collectors.joining(", "))));
            easyBindLabel.textProperty().bind(ebDerivedValueBinding.getOrElse(new ReadOnlyStringWrapper("Nothing to see here, move on")));
            }

            // EasyBind Binding Approach 2
            {
            ObservableList<ModelItemDetail> ebDetailList = EasyBind.select(selectedItemBinding).selectObject(ModelItem::detailsProperty).get();
            ObservableList<ObservableValue<String>> ebDerivedList = EasyBind.map(ebDetailList, i -> derivedBinding(i.aProperty(), i.bProperty()));
            ObservableValue<String> ebDerivedValueBinding = EasyBind.combine(ebDerivedList, stream -> stream.collect(Collectors.joining(", "))).orElse("Nothing to see here, move on");
            easyBindLabel2.textProperty().bind(ebDerivedValueBinding);
            }

            // Subscribe approach
            EasyBind.subscribe(selectedItemBinding, newValue -> {
                if (newValue != null) {
                        ObservableList<ObservableValue<String>> l = EasyBind.map(newValue.getDetails(), i -> derivedBinding(i.aProperty(), i.bProperty()));
                        easyBindLabelSub.textProperty().bind(
                                EasyBind.combine(l, 
                                        strm -> strm.collect(Collectors.joining(", "))
                                ));
                }
            });
            //With this it works as intended, but something feels very wrong about this
             /*
            EasyBind.subscribe(checkShowMore.selectedProperty(), newValue -> {
                if (selectedItemBinding != null) {
                        ObservableList<ObservableValue<String>> l = EasyBind.map(selectedItemBinding.getValue().getDetails(), i -> derivedBinding(i.aProperty(), i.bProperty()));
                        easyBindLabelSub.textProperty().bind(
                                EasyBind.combine(l, 
                                        strm -> strm.collect(Collectors.joining(", "))
                                ));
                }
                });
            */

            // Listener approach
            selectedItemBinding.addListener( (ob, o, n) -> {
                ObservableList<ModelItemDetail> ebDetailList = n.getDetails();
                ObservableList<ObservableValue<String>> ebDerivedList = EasyBind.map(ebDetailList, i -> derivedBinding(i.aProperty(), i.bProperty()));
                ObservableValue<String> ebDerivedValueBinding = EasyBind.combine(ebDerivedList, stream -> stream.collect(Collectors.joining(", "))).orElse("Nothing to see here, move on");
                easyBindLabelLis.textProperty().bind(ebDerivedValueBinding);                
            });





            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private ObservableValue<String> derivedBinding(ObservableValue<String> aProp, ObservableValue<String> bProp) {
        return EasyBind.combine(aProp, bProp, checkShowMore.selectedProperty(), 
                (a, b, s) -> Boolean.TRUE.equals(s) ? new String(a + " <" + b + ">") : a);
    }   

    private ModelItem newModelItem(int number) { 
        ModelItem item = new ModelItem();
        item.itemNumber = number+1;
        for (int i=0;i<2;i++) { 
            ModelItemDetail detail = new ModelItemDetail();
            detail.setA("A" + (i+item.itemNumber));
            detail.setB("B" + (i+item.itemNumber));
            item.getDetails().add(detail);
        }
        return item;
    }

    /** GUI Model class */ 
    private static class MasterDetailModel {
        private ObjectProperty<ModelItem> selectedItemProperty = new SimpleObjectProperty<>();
        public ObjectProperty<ModelItem> selectedItemProperty() { return selectedItemProperty; }
        public ModelItem getSelectedItem() { return selectedItemProperty.getValue(); }
        public void setSelectedItem(ModelItem item) { selectedItemProperty.setValue(item); }
    }

    /** Domain Model class */
    private static class ModelItem { 
        int itemNumber;
        private ListProperty<ModelItemDetail> detailsProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
        public ListProperty<ModelItemDetail> detailsProperty() { return detailsProperty; }
        public ObservableList<ModelItemDetail> getDetails() { return detailsProperty.getValue(); }
        public void setDetails(List<ModelItemDetail> details) { detailsProperty.setValue(FXCollections.observableList(details)); }
        public String toString() { return "Item " + itemNumber; }
    }

    /** Domain Model class */
    private static class ModelItemDetail {
        private StringProperty aProperty = new SimpleStringProperty();
        public StringProperty aProperty() { return aProperty; }
        public String getA() { return aProperty.get(); }
        public void setA(String a) { aProperty.set(a); }

        private StringProperty bProperty = new SimpleStringProperty();
        public StringProperty bProperty() { return bProperty; }
        public String getB() { return bProperty.get(); }
        public void setB(String b) { bProperty.set(b); }
    }
}

更新:我取得了一些进展。

以下代码工作正常,但mysteriouysly仍然只能继续监听CheckBox上的第一个更改:

ListProperty<ModelItemDetail> obsList = new SimpleListProperty<>(FXCollections.observableArrayList(i->new Observable[] { i.aProperty(), i.bProperty(), checkShowMore.selectedProperty()}));
obsList.bind(selectedItemBinding.flatMap(ModelItem::detailsProperty));
ObservableList<ModelItemDetail> ebDetailList = obsList; // WHY ??
ObservableList<ObservableValue<String>> ebDerivedList = EasyBind.map(ebDetailList, i -> derivedBinding(i.aProperty(), i.bProperty()));
ObservableValue<String> ebDerivedValueBinding = EasyBind.combine(ebDerivedList, stream -> stream.collect(Collectors.joining(", "))).orElse("Nothing to see here, move on");
labelPlayground.textProperty().bind(ebDerivedValueBinding);

显然,我遇到麻烦的主要原因是因为我没有看到如何使用EasyBind流畅的API从绑定的当前ObservableList获取selectedItem。声明本地ListProperty并将其绑定到所选项目我可以利用ListProperty作为ObservableList。我认为EasyBind在某个地方并不适用。类型信息的感觉在某处迷路了。我无法在最后一段代码中汇总所有这些变量,而且我不明白为什么EasyBind.map()会在最后一段代码中接受ebDetailList,但却不接受{ {1}}。

所以,现在的问题是,为什么这个绑定只是第一次收听CheckBox事件? obsList支持列表中的提取程序不执行任何操作。我想ListProperty正在用模型中的一个替换支持列表,它没有提取器。

2 个答案:

答案 0 :(得分:3)

如果我理解正确,您希望标签显示所选ModelItem的文字,该文字由其包含的所有ModelItemDetail组成。每当ModelItemDetail被添加或删除时,此文本都应更新,在其ab属性的任何ModelItemDetailModelItemDetail属性时更新列表已更新。

您不需要外部库来实现此1级深度绑定(a - &gt; bModelItemDetail)。 ObservableList报告对ListProperty<ModelItemDetail> detailsProperty = new SimpleListProperty<>( FXCollections.observableArrayList(i -> new Observable[]{i.aProperty(), i.bProperty()})); 列表的更改。 extractor

可以报告对列表中项目属性的更改
ListProperty

事实上,你不需要一个ObservableList,一个简单的ModelItem就足够了。

在下面的例子中,

  • ListView显示在ModelItemDetail中。它初始化为3 a,其中包含bModelItemDetail个属性。
  • 底部的文字标签显示合并的CheckBox s。
  • 的文字
  • 顶部的b确定是否显示b属性。请注意,即使未选中,也会继续报告对ModelItemDetail的更改(但未显示)。
  • &#34;添加项目详情&#34;右侧的按钮会将另一个随机编号的ObservableList添加到列表中。此更改将立即反映在a
  • &#34;改变一些A&#34;右侧的按钮将设置列表中随机选择的ModelItemDetail的{​​{1}}属性的值。此更改将立即通过ObservableList提取程序反映出来。
public class Main extends Application {

    public Main() {}

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

    @Override
    public void start(Stage stage) throws Exception {
        // Mock initial data
        ModelItem item = new ModelItem();
        ModelItemDetail mid1 = new ModelItemDetail();
        mid1.setA("a1");
        mid1.setB("b1");
        ModelItemDetail mid2 = new ModelItemDetail();
        mid2.setA("a2");
        mid2.setB("b2");
        ModelItemDetail mid3 = new ModelItemDetail();
        mid3.setA("a3");
        mid3.setB("b3");
        ObservableList<ModelItemDetail> details = item.getDetails();
        details.add(mid1);
        details.add(mid2);
        details.add(mid3);

        // Create binding

        CheckBox showB = new CheckBox("Show b");
        Label label = new Label();

        label.textProperty().bind(Bindings.createStringBinding(() -> {
            return details.stream()
                .map(mid ->
                    Boolean.TRUE.equals(showB.isSelected()) ? new String(mid.getA() + " <" + mid.getB() + ">") : mid.getA()
                ).collect(Collectors.joining(", "));
        }, details, showB.selectedProperty()));

        // Create testing components

        Button add = new Button("Add item detail");
        add.setOnAction(e -> {
            Random r = new Random();
            int i = r.nextInt(100) + 3;
            ModelItemDetail mid = new ModelItemDetail();
            mid.setA("a" + i);
            mid.setB("b" + i);
            details.add(mid);
        });
        Button changeA = new Button("Change some A");
        changeA.setOnAction(e -> {
            Random r = new Random();
            ModelItemDetail detail = details.get(r.nextInt(details.size()));
            detail.setA("a" + r.nextInt(100) + 3);
        });

        // Display everything

        BorderPane pane = new BorderPane();
        ListView<ModelItem> list = new ListView<>();
        list.getItems().add(item);
        pane.setCenter(list);
        pane.setRight(new VBox(add, changeA));
        pane.setTop(showB);
        pane.setBottom(label);
        stage.setScene(new Scene(pane));
        stage.show();
    }

    private static class ModelItem {
        int itemNumber;
        private ObservableList<ModelItemDetail> detailsProperty = FXCollections.observableArrayList(i -> new Observable[]{i.aProperty(), i.bProperty()});
        public ObservableList<ModelItemDetail> getDetails() { return detailsProperty; }
        @Override public String toString() { return "Item " + itemNumber; }
    }

    /** Domain Model class */
    private static class ModelItemDetail {
        private StringProperty aProperty = new SimpleStringProperty();
        public StringProperty aProperty() { return aProperty; }
        public String getA() { return aProperty.get(); }
        public void setA(String a) { aProperty.set(a); }

        private StringProperty bProperty = new SimpleStringProperty();
        public StringProperty bProperty() { return bProperty; }
        public String getB() { return bProperty.get(); }
        public void setB(String b) { bProperty.set(b); }
    }
}

您可以向ModelItem添加更多ListView,并让标签显示所选标签的文字。

答案 1 :(得分:0)

经过一段时间的练习并熟悉Bindings,Properties和Observables后,我想出了我想要的东西。一个简单,功能强大,简洁且类型安全的EasyBind表达式,不需要监听器,复制或显式声明绑定依赖关系或提取器。绝对看起来比Bindings API版本要好得多。

 labelWorking.textProperty().bind(
    selectedItemBinding
    .flatMap(ModelItem::detailsProperty)
    .map(l -> derivedBinding(l))
    .flatMap(l -> EasyBind.combine(
             l, stream -> stream.collect(Collectors.joining(", "))))
    );

private ObservableList<ObservableValue<String>> derivedBinding(ObservableList<ModelItemDetail> l) { 
        return l.stream()
                .map(c -> derivedBinding(c.aProperty(), c.bProperty()))
                .collect(Collectors.toCollection(FXCollections::observableArrayList));
    }

Eclipse / javac中有类型推断显然存在一些错误。当我试图找到让IDE指导我的正确表达时,这无助于弄清楚事情。

为了完整起见,带有工作绑定的MVCE:

package mcve.javafx;

import java.util.List;
import java.util.stream.Collectors;

import org.fxmisc.easybind.EasyBind;
import org.fxmisc.easybind.monadic.MonadicBinding;

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.binding.Binding;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    private CheckBox checkShowMore;

    @Override
    public void start(Stage primaryStage) {
        try {


            // Initialize model
            MasterDetailModel mdModel = new MasterDetailModel();
            ObservableList<ModelItem> itemsList = FXCollections.observableArrayList();
            for (int i=0;i<5;i++) { itemsList.add(newModelItem(i)); }

            MonadicBinding<ModelItem> selectedItemBinding = EasyBind.monadic(mdModel.selectedItemProperty()).orElse(new ModelItem());

            // Master
            ListView<ModelItem> listView = new ListView<ModelItem>();
            listView.setItems(itemsList);
            listView.setPrefHeight(150);
            mdModel.selectedItemProperty().bind(listView.getSelectionModel().selectedItemProperty());

            //Detail
            checkShowMore = new CheckBox();
            checkShowMore.setText("Show more details");
            VBox detailVBox = new VBox();           
            Label apiLabel = new Label();
            Label labelPlayground = new Label();
            detailVBox.getChildren().addAll(
                    checkShowMore, 
                    new TitledPane("API Binding", apiLabel), 
                    new TitledPane("EasyBind", labelPlayground)
            );


            // Scene
            Scene scene = new Scene(new VBox(listView, detailVBox),400,400);
            primaryStage.setScene(scene);
            primaryStage.setTitle("JavaFX/EasyBind MVCE");

            // --------------------------
            // -------- BINDINGS --------
            // --------------------------

            // API Label Binding

            apiLabel.textProperty().bind(Bindings.createStringBinding( 
                () -> selectedItemBinding.getValue().getDetails().stream()
                    .map(i -> derivedBinding(i.aProperty(), i.bProperty()))
                    .map(v->v.getValue())
                    .collect(Collectors.joining(", "))
                , mdModel.selectedItemProperty(), checkShowMore.selectedProperty()));

            // EasyBind non-working attempt
            /*
            ListProperty<ModelItemDetail> obsList = new SimpleListProperty<>(FXCollections.observableArrayList(i->new Observable[] { i.aProperty(), i.bProperty(), checkShowMore.selectedProperty()}));
            obsList.bind(selectedItemBinding.flatMap(ModelItem::detailsProperty));
            ObservableList<ModelItemDetail> ebDetailList = obsList; // WHY ??
            ObservableList<ObservableValue<String>> ebDerivedList = EasyBind.map(ebDetailList, i -> derivedBinding(i.aProperty(), i.bProperty()));
            ObservableValue<String> ebDerivedValueBinding = EasyBind.combine(ebDerivedList, stream -> stream.collect(Collectors.joining(", "))).orElse("Nothing to see here, move on");
            labelPlayground.textProperty().bind(ebDerivedValueBinding);
            */

            // Working EasyBind Binding
            labelPlayground.textProperty().bind(
                    selectedItemBinding
                    .flatMap(ModelItem::detailsProperty)
                    .map(l -> derivedBinding(l))
                    .flatMap(l -> EasyBind.combine(l, stream -> stream.collect(Collectors.joining(", "))))
                    );

            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private ObservableList<ObservableValue<String>> derivedBinding(ObservableList<ModelItemDetail> l) { 
        return l.stream()
                .map(c -> derivedBinding(c.aProperty(), c.bProperty()))
                .collect(Collectors.toCollection(FXCollections::observableArrayList));
    }

    private Binding<String> derivedBinding(ObservableValue<String> someA, ObservableValue<String> someB ) { 
        return EasyBind.combine(someA, someB, checkShowMore.selectedProperty(), 
                        (a, e, s) -> a + (Boolean.TRUE.equals(s) ? " <" + e + ">" : ""));
    }

    private ModelItem newModelItem(int number) { 
        ModelItem item = new ModelItem();
        item.itemNumber = number+1;
        for (int i=0;i<2;i++) { 
            ModelItemDetail detail = new ModelItemDetail("A" + (i+item.itemNumber), "B" + (i+item.itemNumber));
            item.getDetails().add(detail);
        }
        return item;
    }

    /** GUI Model class */ 
    private static class MasterDetailModel {
        private ObjectProperty<ModelItem> selectedItemProperty = new SimpleObjectProperty<>();
        public ObjectProperty<ModelItem> selectedItemProperty() { return selectedItemProperty; }
        public ModelItem getSelectedItem() { return selectedItemProperty.getValue(); }
        public void setSelectedItem(ModelItem item) { selectedItemProperty.setValue(item); }
    }

    /** Domain Model class */
    private static class ModelItem { 
        int itemNumber;
        private ListProperty<ModelItemDetail> detailsProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
        public ListProperty<ModelItemDetail> detailsProperty() { return detailsProperty; }
        public ObservableList<ModelItemDetail> getDetails() { return detailsProperty.getValue(); }
        public void setDetails(List<ModelItemDetail> details) { detailsProperty.setValue(FXCollections.observableList(details)); }
        public String toString() { return "Item " + itemNumber; }
    }

    /** Domain Model class */
    private static class ModelItemDetail {

        public ModelItemDetail(String a, String b) { 
            setA(a);
            setB(b);
        }

        private StringProperty aProperty = new SimpleStringProperty();
        public StringProperty aProperty() { return aProperty; }
        public String getA() { return aProperty.get(); }
        public void setA(String a) { aProperty.set(a); }

        private StringProperty bProperty = new SimpleStringProperty();
        public StringProperty bProperty() { return bProperty; }
        public String getB() { return bProperty.get(); }
        public void setB(String b) { bProperty.set(b); }
    }
}