2015-10-15 106 views
6

我想将JavaFX Label.textPropertyint的值绑定。JavaFX绑定带int值的标签

我试过

Label.textProperty().bindBidirectional(new SimpleIntegerProperty(myInt), 
                 new NumberStringConverter()); 

Label().textProperty().bindBidirectional(new SimpleIntegerProperty(myInt), 
                   new DecimalFormat()); 

但我总是得到NullPointerException异常

我该如何解决?

+0

任何原因在Label上使用双向绑定? – ItachiUchiha

+0

@IchichiUchiha可能是因为它允许提供一个'StringConverter'。我试过之前,我发现你可以使用'integerProperty.asString()'与单向绑定,正如你在答案中指出的那样。 – Ruben9922

回答

9

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

label.textProperty().bind(new SimpleIntegerProperty(integer).asString()); 

如果你有一个IntegerProperty,你可以直接使用它

label.textProperty().bind(integerProperty.asString()); 
+3

附加提示:您可以使用'asString(String format)'的重载版本,该版本采用格式来进行数字的附加格式化。 –