JavaFX绑定带有int值的Label

Ska*_*pka 10 java binding javafx

我要绑定的JavaFX Label.textPropertyint价值.

我试过例如

Label.textProperty().bindBidirectional(new SimpleIntegerProperty(myInt), 
                                                      new NumberStringConverter());
Run Code Online (Sandbox Code Playgroud)

要么

Label().textProperty().bindBidirectional(new SimpleIntegerProperty(myInt), 
                                                              new DecimalFormat());
Run Code Online (Sandbox Code Playgroud)

但我总是得到NullPointerException.

我该如何解决?

Ita*_*iha 15

如果你有一个int,你可以从它创建一个SimpleIntegerProperty,然后使用asString()它:

label.textProperty().bind(new SimpleIntegerProperty(integer).asString());
Run Code Online (Sandbox Code Playgroud)

如果您有IntegerProperty,则可以直接使用它

label.textProperty().bind(integerProperty.asString());
Run Code Online (Sandbox Code Playgroud)

  • 附加提示:您可以使用`asString(String format)`的重载版本,它采用格式对数字进行额外的格式化. (4认同)