5 Replies Latest reply: Jul 27, 2012 4:52 AM by 947038 RSS

    Synchronize 2 ObservableLists


      I'm trying to figure out how to synchronize 2 lists.

      Let's say I have a custom Control with a method:
      ObservableList<Foo> getFoos();
      In the Skin, the node has a method like:
      ObservableList<Bar> getBars();
      In the Skin, I can define a method like:
      Bar createBar(Foo foo)
      In the constructor I could add a listener like this:
      control.getFoos().addListener(new ListChangeListener<Foo>() {
                  public void onChanged(ListChangeListener.Change<? extends Foo> change) {
                      while (change.next()) {
                          if (change.wasPermutated()) {
                              for (int i = change.getFrom(); i < change.getTo(); ++i) {
                                  // TODO: ???
                          } else if (change.wasUpdated()) {
                              // TODO: ???
                          } else if (change.wasRemoved()) {
                              for (Foo remitem : change.getRemoved()) {
                          } else if (change.wasAdded()) {
                              for (int index = change.getFrom(); index < change.getTo(); index++) {
                                  myNode.getBars().add(index, creatBar(change.getList().get(index));
                          } else if (change.wasReplaced()) {
                              // TODO: ???
      I'm especially confused about wasReplaced, wasUpdated and that many methods are not mutual exclusive.

      And e.g. what events are fired if List.clear()/ removeAll() etc. are fired? One change event? Several? The documentation is very unclear in this regard.
        • 1. Re: Synchronize 2 ObservableLists
          A google search revealed this link =>

          Which deeplinks into the a preview of Chapter 6 "JavaFX collections" of the Pro JavaFX2 book which contains an executable example that is an excellent overview of JavaFX Collections and thoroughly answers your question.

          The Pro JavaFX2 book is available for purchase in printed form from:
          • 2. Re: Synchronize 2 ObservableLists
            You are looking for Bindings.bindContent(list1, list2);

            (available with JavaFX 2.1)
            • 3. Re: Synchronize 2 ObservableLists
              @jsmith Thanks for the hint. Could be helpful to understand these change events.
              • 4. Re: Synchronize 2 ObservableLists
                @csh great, I was hoping for something like this. It will work at least for the case where the lists have the same content type. It would be great if there were a version which allowed to convert from one content type to another.
                • 5. Re: Synchronize 2 ObservableLists
                  A great and helpful book, though it says nothing about "wasUpdated()" (as you can see on page 232 in the UML diagram; Figure 6-1), since wasUpdated() was introduced with JavaFX 2.1.

                  So I wonder, what would be the supposed way, to listen contentwise to a list. In other words, how to be notified about changes IN a list a element, without externally registering listeners.
                  I thought, this can be done only, if the list elements implement Observable, or any sub-interface. Unfortunately, SimpleListProperty does not seem to have implemented wasUpdated() for such list elements. So I was not able to answer my above question :)