Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bidirectional bindings of different properties

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:

  1. public static void bindBidirectional(Property stringProperty,
    Property otherProperty, StringConverter converter)

  2. 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);    
    }
}
like image 732
dethlef1 Avatar asked Jan 03 '13 11:01

dethlef1


2 Answers

Simple matter of type confusion

Bindings.bindBidirectional(textProp, intProp, new IntegerStringConverter());

Should be:

Bindings.bindBidirectional(textProp, intProp, new NumberStringConverter());
like image 104
Emily L. Avatar answered Oct 10 '22 06:10

Emily L.


I tried your code in Eclipse and had to cast the converter. Then everything looks ok:

public class BiderectionalBinding {

    public static void main(String[] args) {
        SimpleIntegerProperty intProp = new SimpleIntegerProperty();
        SimpleStringProperty textProp = new SimpleStringProperty();
        StringConverter<? extends Number> converter =  new IntegerStringConverter();

        Bindings.bindBidirectional(textProp, intProp,  (StringConverter<Number>)converter);

        intProp.set(2);
        System.out.println(textProp);

        textProp.set("8");
        System.out.println(intProp);    
    }
}

The output is:

StringProperty [value: 2]

IntegerProperty [value: 8]

like image 28
Hendrik Ebbers Avatar answered Oct 10 '22 04:10

Hendrik Ebbers