2 Replies Latest reply: Apr 26, 2013 4:25 AM by 1005686 RSS

    No change events fired on ListProperty at all after adding second listener

    1005686
      Hello all,

      I got a strange problem with ChangeListeners on a ListProperty: Having only one listener, change events are fired as expected. After adding a second listener, no change events are fired at all. Please see the example code below:
      package test;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import javafx.beans.property.SimpleListProperty;
      import javafx.beans.value.ChangeListener;
      import javafx.beans.value.ObservableValue;
      import javafx.collections.FXCollections;
      
      public class ListListenerTest {
      
           public static void main(String[] args) {
      
                SimpleListProperty<Integer> listProp = new SimpleListProperty<Integer>(FXCollections.observableArrayList(new ArrayList<Integer>()));
                
                listProp.addListener(new ChangeListener<List<Integer>>() {
                     
                     @Override
                     public void changed(ObservableValue<? extends List<Integer>> paramObservableValue, List<Integer> paramT1, List<Integer> paramT2) {
                          System.out.println("List listener 1 notified.");
                     }
                });
                
                //This listener breaks stuff
                listProp.addListener(new ChangeListener<List<Integer>>() {
                     
                     @Override
                     public void changed(ObservableValue<? extends List<Integer>> paramObservableValue, List<Integer> paramT1, List<Integer> paramT2) {
                          System.out.println("List listener 2 notified.");
                     }
                });
                //
                
                System.out.println("Setting list.");
                listProp.setAll(1,2,3);
                System.out.println("Value: " + listProp.getValue());
           }
           
      }
      The code above prints the following:
      Setting list.
      Value: [1, 2, 3]
      Removing the second listener results in the following (expected) output:
      Setting list.
      List listener 1 notified.
      Value: [1, 2, 3]
      Using a SimpleIntegerProperty instead results in both listeners to be notified. I would expect the same thing for the ListProperty.

      After looking for hours in all the wrong places until I arrived at this minimal example, I would really be grateful for any hint.
      I did not find anything in the forum or JIRA, except Adding Invalidation and Change listeners to a Listproperty , which might be related.

      Thanks,
      Reinhard
        • 1. Re: No change events fired on ListProperty at all after adding second listener
          csh
          This seems to be a bug in JavaFX 2.2.

          I just tested it with 2.2 and it doesn't work.

          Then I tested the same code with JavaFX 8.0 b86 and it does work:

          Setting list.
          List listener 1 notified.
          List listener 2 notified.
          Value: [1, 2, 3]

          Maybe you can workaround by using ListChangeListener directly on the list?
          • 2. Re: No change events fired on ListProperty at all after adding second listener
            1005686
            Thank you very much for your fast response. This was really driving me mad. :(

            In the meantime, I filed a bug report under: [https://javafx-jira.kenai.com/browse/RT-30010] and tried to used an InvalidationListener instead of the second ChangeListener. This results in the notification of the InvalidationListener only. :(

            Also thanks for the suggestion to use a ListChangeListener. I would have liked to avoid this, as this involves some type checks. The type of property (List, Double,...) is determined at runtime by a configuration file, so using listeners that are available for all types of properties would be easier. Though, as there seem to be some unforeseeable interactions between different listeners on a single property, I most likely have to use this approach...

            Regards,
            Reinhard