Binding 如何使用格式化程序绑定属性

Binding 如何使用格式化程序绑定属性,binding,javafx,Binding,Javafx,我有一个标签和一个双重属性。我想输出带有后缀“$”的值,I。E"200.50 $". 如何使用JavaFX实现这一点?我考虑过这样使用绑定: @FXML Label label; DoubleProperty value; ... Bindings.bindBidirectional( label.textProperty(), valueProperty(), NumberFormat.getInstance()); public class MoneyStringConverter e

我有一个标签和一个双重属性。我想输出带有后缀“$”的值,I。E"200.50 $". 如何使用JavaFX实现这一点?我考虑过这样使用绑定:

@FXML Label label;
DoubleProperty value;
...
Bindings.bindBidirectional( label.textProperty(), valueProperty(), NumberFormat.getInstance());
  public class MoneyStringConverter extends StringConverter<Number> {

    String postFix = " $";
    NumberFormat formatter = numberFormatInteger; 

    @Override
    public String toString(Number value) {

        return formatter.format( value) + postFix;

    }

    @Override
    public Number fromString(String text) {

      try {

        // we don't have to check for the symbol because the NumberFormat is lenient, ie 123abc would result in the value 123
        return formatter.parse( text);

      } catch( ParseException e) {
        throw new RuntimeException( e);
      }

    }

  }
但是还没有找到将“$”附加到文本的方法


非常感谢你的帮助

您可以使用

label.textProperty().bind(Bindings.concat(value).concat("$"));

这样的办法应该行得通

Bindings.format("%.2f $", myDoubleProperty.getValue());
现在你可以把它绑起来了

 label.textProperty.bind(Bindings.format("%.2f $", myDoubleProperty.getValue());
如果要使用NumberFormat实例,只需使用它格式化myDoubleProperty的值

NumberFormat formatter = NumberFormat.getInstance();
formatter.setSomething();
formatter.format(myDoubleProperty.getValue());
现在将其添加到Bindings.format中

编辑:


包括ItachiUchiha的输入。

多亏了ItachiUchiha和NDY,我找到了正确的答案。我必须使用如下的StringConverter:

@FXML Label label;
DoubleProperty value;
...
Bindings.bindBidirectional( label.textProperty(), valueProperty(), NumberFormat.getInstance());
  public class MoneyStringConverter extends StringConverter<Number> {

    String postFix = " $";
    NumberFormat formatter = numberFormatInteger; 

    @Override
    public String toString(Number value) {

        return formatter.format( value) + postFix;

    }

    @Override
    public Number fromString(String text) {

      try {

        // we don't have to check for the symbol because the NumberFormat is lenient, ie 123abc would result in the value 123
        return formatter.parse( text);

      } catch( ParseException e) {
        throw new RuntimeException( e);
      }

    }

  }

难道没有像Bindings.format(“%d$”,propertyValue)这样的东西吗?最好使用
label.textProperty().bind(Bindings.format(“%.2f$”,value))
,因为它是一个
DoubleProperty
谢谢,但我需要使用格式化程序,而不是格式字符串。不确定您到底想做什么,但您可以格式化您的属性值,然后使用Bindings.format添加$?请参阅我的编辑。当我调用setValue(123)时,我希望隐式地将标签更新为“123$”。除非在更改侦听器中使用外部格式化程序,否则不会调用它。我刚发现有一个类似于StringConverter的东西。我想我得用那个。我会把密码作为答案贴出来。谢谢你们的帮助。这不是解决办法,但它把我带到了那里。太好了:)我有点误解了你的想法,但你找到了自己的路,这很好!谢谢,但是您知道如何在NumberFormat实例中使用它吗?我不确定NumberFormat是否可以这样做,因为NumberFormat有预定义的货币前缀/后缀表达式。它似乎与我发布的MoneyStringConverter一起工作。或者你认为这有问题吗?主要目标是拥有一个setValue()方法,所有内容都应该隐式更新,而不必使用ChangeListener或e。G多个updateTextField()方法。我没有看到您的答案,很高兴看到StringConverter的实现。唯一让我困扰的是,既然货币总是十进制格式,为什么不使用
DecimalFormat
而不是
NumberFormat
?这只是一个示例格式化程序。我需要它也显示e。G带2位小数的度数,一种以“h”为后缀的小时的日期时间格式,等等。但你是对的,如果涉及金钱,应该使用DecimalFormat。我仍然认为,这种方法不是很好(对于单向绑定)。我更喜欢
label.textProperty().bind(doubleProperty().asString(%.2f))-更短,更易于阅读。