2 Replies Latest reply: Feb 5, 2013 2:19 PM by James_D RSS

    Binds 2 Observable Lists throwing java.lang.UnsupportedOperationException

    989211
      Below is a simple code

      private ListProperty<String> list = new SimpleListProperty<>();
      private ListProperty<String> list2 = new SimpleListProperty<>();

      Bindings.bindContentBidirectional(list,list2);

      I want to bind both the Lists. But for some reason it throws: java.lang.UnsupportedOperationException at the Bindings Line.

      What is the issue with it?
        • 1. Re: Binds 2 Observable Lists throwing java.lang.UnsupportedOperationException
          shakir.gusaroff
          Hi. The arguments (list1 and list2) should be observableList

          http://docs.oracle.com/javafx/2/api/javafx/beans/binding/Bindings.html#bindContentBidirectional%28javafx.collections.ObservableList,%20javafx.collections.ObservableList%29
          • 2. Re: Binds 2 Observable Lists throwing java.lang.UnsupportedOperationException
            James_D
            SimpleListProperty implements ObservableList, so the types are just fine.

            The problem is that the default constructor new SimpleListProperty() creates a ListProperty that wraps an empty list; effectively it's equivalent to
            new SimpleListProperty(FXCollections.emptyObservableList());

            The list returned by FXCollections.emptyObservableList() is unmodifiable; the binding will attempt to call setAll(...) on one of the lists (passing the content of the other). Of course, an unmodifiable list will throw an UnsupportedOperationException when you try to set elements in it.

            You need to create the SimpleListProperty with a modifiable list:
            private ListProperty<String> list = new SimpleListProperty<>(FXCollections.<String>observableArrayList());
            private ListProperty<String> list2 = new SimpleListProperty<>(FXCollections.<String>observableArrayList());
            
            Bindings.bindContentBidirectional(list,list2);