8

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);    
    }
}
Kai
  • 38,985
  • 14
  • 88
  • 103
dethlef1
  • 81
  • 1
  • 1
  • 2
  • 5
    If your question has been resolved. You should accept an answer. – Emily L. Mar 16 '15 at 12:04
  • 1
    Is there a more generic answer to this question (or a more generic question, for that matter)? I'd like to bind two properties, one of which are not a `String`. – Kröw Jun 16 '19 at 20:07

4 Answers4

17

Simple matter of type confusion

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

Should be:

Bindings.bindBidirectional(textProp, intProp, new NumberStringConverter());
Emily L.
  • 5,673
  • 2
  • 40
  • 60
3

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]

Hendrik Ebbers
  • 2,570
  • 19
  • 34
  • Perfect, thank you. I got also other helpful answers at https://forums.oracle.com/forums/message.jspa?messageID=10773242 – dethlef1 Jan 03 '13 at 13:51
  • For more informations about the JavaFX Property API read http://docs.oracle.com/javafx/2/api/javafx/beans/binding/Bindings.html – Hendrik Ebbers Jan 10 '13 at 17:23
3

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..

package de.ludwig.binding.model;

import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;

import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class BidirectionalBinding {

    public static void main(String[] args) {
        SimpleIntegerProperty intProp = new SimpleIntegerProperty();
        SimpleStringProperty textProp = new SimpleStringProperty();

        Bindings.bindBidirectional(textProp, intProp, new Format() {

            @Override
            public StringBuffer format(Object obj, StringBuffer toAppendTo,
                    FieldPosition pos) {
                return toAppendTo.append(obj.toString());
            }

            @Override
            public Object parseObject(String source, ParsePosition pos) {
                return Integer.parseInt(source);
            }

        });

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

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

The only way to get things working as expected was to implement StringConverter as recommended by Hendrik Ebbers. Thank you for this tip!

  • 2
    Why dont you add that part into your code so this answer become useful for others. Other wise this is not a good answer if some one is looking for that same issue. – BBdev Mar 25 '13 at 14:11
0

I think that is a bug. Anyway you can workaround like:

StringConverter sc = new IntegerStringConverter();
Bindings.bindBidirectional(textProp, intProp, sc);
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153