Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

java - 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);    
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Simple matter of type confusion

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

Should be:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...