I just tried to bind a Integer and a String property. After some googling this should be possible with one of the two provided methods:
public static void bindBidirectional(Property stringProperty,
Property otherProperty, StringConverter converter)public static void bindBidirectional(Property stringProperty,
Property otherProperty, java.text.Format format)
Unluckily this does not seem to work for me. What am I doing wrong?
import java.text.Format;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.util.converter.IntegerStringConverter;
public class BiderectionalBinding {
public static void main(String[] args) {
SimpleIntegerProperty intProp = new SimpleIntegerProperty();
SimpleStringProperty textProp = new SimpleStringProperty();
Bindings.bindBidirectional(textProp, intProp, new IntegerStringConverter());
intProp.set(2);
System.out.println(textProp);
textProp.set("8");
System.out.println(intProp);
}
}
Simple matter of type confusion
Should be:
I tried your code in Eclipse and had to cast the converter. Then everything looks ok:
The output is:
StringProperty [value: 2]
IntegerProperty [value: 8]
i had a similar problem. I tried to convert a string into a File-Object and back. But i used Bindings.bindBidirectional(...,...,java.text.Format). The conversion from the string to file worked as expected but in the other direction the result was null. I tried it with your example, same Result! I think there is a bug in the binding mechanism, or maybe my implementation of java.text.Format is wrong..
The only way to get things working as expected was to implement StringConverter as recommended by Hendrik Ebbers. Thank you for this tip!
I think that is a bug. Anyway you can workaround like: