2015-10-15 12 views
6

Voglio associare JavaFX Label.textProperty con il valore int.Associazione JavaFX Etichetta con valore int.

Ho provato ad es.

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

o

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

ma ottengo sempre NullPointerException.

Come posso risolvere il problema?

+0

alcuna motivazione per usare il binding bidirezionale sull'etichetta? – ItachiUchiha

+0

@ItachiUchiha Forse perché consente di fornire un 'StringConverter'. L'ho provato prima di scoprire che puoi usare 'integerProperty.asString()' con una rilegatura unidirezionale come hai indicato nella tua risposta. – Ruben9922

risposta

9

Se si dispone di un int è possibile creare un SimpleIntegerProperty da esso e quindi utilizzare il asString() su di essa:

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

Se avete un IntegerProperty, è possibile utilizzare direttamente esso

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

Suggerimento aggiuntivo: è possibile utilizzare la versione sovraccaricata di 'asString (String format)' che accetta un formato per eseguire un'ulteriore formattazione del numero. –

Problemi correlati