2 Replies Latest reply on Jul 5, 2013 4:33 PM by Andipa

    Does Bindings.select not work with primitives properties like StringProperty etc?

    Andipa

      My code works when I use an ObjectProperty<String>, it however doesn't work when I use a StringProperty instead.

       

      Don't know if this behaviour of Bindings.select is a bug or expected.

       

      Here's the SSCCE:

       

      The printout is:

      ObjectProperty<String> -> ObjectProperty<String>: abc

      StringProperty -> StringProperty: null

      StringProperty -> ObjectProperty<String>: null

       

      I would expect all cases to print "abc" though

       

      import javafx.beans.binding.Bindings;
      import javafx.beans.property.ObjectProperty;
      import javafx.beans.property.SimpleObjectProperty;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.property.StringProperty;
      
      public class SelectBindingTest {
      
          public static class A {
      
              private StringProperty comment = new SimpleStringProperty(this, "comment");
              private ObjectProperty<String> commentObj = new SimpleObjectProperty<>(this, "commentObj");
      
              String getComment() {
                  return comment.get();
              }
      
              StringProperty commentProperty() {
                  return comment;
              }
      
              void setComment(String comment) {
                  this.comment.set(comment);
              }
      
              public String getCommentObj() {
                  return commentObj.get();
              }
      
              public ObjectProperty<String> commentObjProperty() {
                  return commentObj;
              }
      
              public void setCommentObj(String commentObj) {
                  this.commentObj.set(commentObj);
              }
          }
      
          public static class B {
              private ObjectProperty<A> member = new SimpleObjectProperty<>(this, "member");
      
              A getMember() {
                  return member.get();
              }
      
              ObjectProperty<A> memberProperty() {
                  return member;
              }
      
              void setMember(A member) {
                  this.member.set(member);
              }
          }
      
          public static void main(String[] args) {
              B b = new B();
              A member = new A();
              b.setMember(member);
      
              final String expectedString = "abc";
      
      // this works
              SimpleObjectProperty<String> targetStringObjProperty = new SimpleObjectProperty<>("initialTargetText");
              member.setCommentObj("initialSourceText");
              targetStringObjProperty.bind(Bindings.<String>select(b.memberProperty(), "commentObj"));
              member.setCommentObj(expectedString);
              System.out.println("ObjectProperty<String> -> ObjectProperty<String>: " + targetStringObjProperty.get());
      
      // this doesn't work
              SimpleStringProperty targetStringProperty = new SimpleStringProperty("initialTargetText");
              member.setComment("initialSourceText");
              targetStringProperty.bind(Bindings.selectString(b.memberProperty(), "comment"));
              member.setComment(expectedString);
              System.out.println("StringProperty -> StringProperty: " + targetStringProperty.get());
      
      // this doesn't work either
              targetStringObjProperty = new SimpleObjectProperty<>("initialTargetText");
              member.setComment("initialSourceText");
              targetStringObjProperty.bind(Bindings.<String>select(b.memberProperty(), "comment"));
              member.setComment(expectedString);
              System.out.println("StringProperty -> ObjectProperty<String>: " + targetStringObjProperty.get());
          }
      
      }
      
      
      
      

       

      EDIT: sorry for the awful formatting. Can't seem to figure out how to properly insert blank lines.