1

There is a method to convert ObjectProperty<Integer> into IntegerProperty. Here it is:

https://docs.oracle.com/javase/8/javafx/api/javafx/beans/property/IntegerProperty.html#integerProperty-javafx.beans.property.Property-

Why there is no analogous method to convert ObjectProperty<String> to StringProperty? How to convert then?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • 1
    Why do you need to do this? Both `StringProperty` and `ObjectProperty` implement `Property`, which is usually specific enough. The problem with `IntegerProperty` etc. is that they implement `Property` rather than `Property`, `Property` etc. See also http://stackoverflow.com/questions/34620552/why-does-longproperty-implement-propertynumber-but-not-propertylong and https://bugs.openjdk.java.net/browse/JDK-8125218 – Itai Jul 04 '16 at 15:28

1 Answers1

0

Looking at the source for integerProperty, all it does is a bidirectional binding (and unbinding in finalize).

You could copy what integerProperty does, and modify it to use StringProperty:

public static StringProperty makeStringProperty(final Property<String> property) {
    Objects.requireNonNull(property, "Property cannot be null");

    return new StringPropertyBase() {
        {
            bindBidirectional(property);
        }

        @Override
        public Object getBean() {
            return null; // Virtual property, no bean
        }

        @Override
        public String getName() {
            return property.getName();
        }

        @Override
        protected void finalize() throws Throwable {
            try {
                unbindBidirectional(property);
            } finally {
                super.finalize();
            }
        }
    };
}

I've modified it a bit here so it does not use internal packages.

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93