【问题标题】:Bidirectional bindings of different properties不同属性的双向绑定
【发布时间】:2012-12-17 18:23:46
【问题描述】:

我只是尝试绑定一个 Integer 和一个 String 属性。经过一番谷歌搜索后,这应该可以使用提供的两种方法之一:

  1. 公共静态无效绑定双向(属性字符串属性,
    属性otherProperty,StringConverter转换器)

  2. 公共静态无效绑定双向(属性字符串属性,
    属性otherProperty,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);    
    }
}

【问题讨论】:

  • 如果您的问题已经解决。你应该接受一个答案。
  • 这个问题是否有更通用的答案(或者更通用的问题,就此而言)?我想绑定两个属性,其中一个不是String。

标签: java binding properties javafx-2 bidirectional


【解决方案1】:

类型混淆的简单问题

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

应该是:

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

【讨论】:

    【解决方案2】:

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

    输出是:

    StringProperty [值:2]

    IntegerProperty [值:8]

    【讨论】:

    【解决方案3】:

    我遇到了类似的问题。我试图将字符串转换为文件对象并返回。但我使用了 Bindings.bindBidirectional(...,...,java.text.Format)。从字符串到文件的转换按预期工作,但在另一个方向上结果为空。我用你的例子试过了,同样的结果!我觉得绑定机制有bug,或者我的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。谢谢你的提示!

    【讨论】:

    • 为什么不将该部分添加到代码中,以便此答案对其他人有用。否则,如果有人正在寻找同样的问题,这不是一个好的答案。
    【解决方案4】:

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

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

    【讨论】:

    • 您的意思是它是 Java、OP 的代码、OP 的 IDE 还是其他地方的错误?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多