Java 不同属性的双向绑定

Java 不同属性的双向绑定,java,binding,properties,javafx-2,bidirectional,Java,Binding,Properties,Javafx 2,Bidirectional,我只是尝试绑定一个整数和一个字符串属性。在谷歌搜索之后,这应该可以通过提供的两种方法之一实现: 公共静态无效绑定(属性stringProperty, 属性(其他属性,StringConverter转换器) 公共静态无效绑定(属性stringProperty, 属性(其他属性,java.text.Format格式) 不幸的是,这似乎对我不起作用。我做错了什么 import java.text.Format; import javafx.beans.binding.Bindings; import

我只是尝试绑定一个整数和一个字符串属性。在谷歌搜索之后,这应该可以通过提供的两种方法之一实现:

  • 公共静态无效绑定(属性stringProperty,
    属性(其他属性,StringConverter转换器)

  • 公共静态无效绑定(属性stringProperty,
    属性(其他属性,java.text.Format格式)

  • 不幸的是,这似乎对我不起作用。我做错了什么

    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);    
        }
    }
    

    我认为这是一个错误。无论如何,您可以像这样解决问题:

    StringConverter sc = new IntegerStringConverter();
    Bindings.bindBidirectional(textProp, intProp, sc);
    

    我在Eclipse中尝试了您的代码,并且必须强制转换转换器。然后一切看起来都很好:

    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);    
        }
    }
    
    公共类双方向绑定{
    公共静态void main(字符串[]args){
    SimpleIntegerProperty intProp=新的SimpleIntegerProperty();
    SimpleStringProperty textProp=新SimpleStringProperty();
    
    StringConverter我也遇到了类似的问题。我试图将字符串转换为文件对象并返回。但我使用了Bindings.bindBidirectional(…,…,java.text.Format)。从字符串到文件的转换按预期进行,但在另一个方向,结果为空。我用您的示例进行了尝试,结果相同!我认为绑定机制中存在错误,或者可能是我对java.text.Format的实现错误

    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);    
        }
    }
    

    让事情按预期运行的唯一方法是按照Hendrik Ebbers的建议实现StringConverter。感谢您提供此技巧!

    简单的类型混淆问题

    Bindings.bindBidirectional(textProp, intProp, new IntegerStringConverter());
    
    应该是:

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

    很好,谢谢。我在上还得到了其他有用的答案,了解更多关于JavaFX属性API的信息,请阅读为什么不将该部分添加到您的代码中,以便此答案对其他人有用。其他方面,如果有人在寻找同一问题,这不是一个好答案。如果您的问题已得到解决,您应该接受答案。是否有这个问题的更一般的答案(或者更一般的问题)?我想绑定两个属性,其中一个不是
    字符串
    。你的意思是它是Java、OP的代码、OP的IDE还是其他地方的bug?