Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX binding Label with int value

I want to bind JavaFX Label.textProperty with int value.

I tried e.g.

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

or

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

But I always get NullPointerException.

How can I fix it?

like image 995
Skartepka Avatar asked Oct 15 '15 10:10

Skartepka


People also ask

How do you initialize a Label in JavaFX?

Syntax to Initialize JavaFX label is: Label lbl = new Label(); Here, the constructor can be of parameterized and non-parameterized, which depends on the requirement.

How do I change the text in a JavaFX Label?

You can change the text of a label using its setText() method. This can be done while the application is running. Here is an example of setting the text of a JavaFX Label: label.

What is the difference between text and Label in JavaFX?

A Text is a geometric shape (like a Rectangle or a Circle), while Label is a UI control (like a Button or a CheckBox). In Swing, geometric shapes were restricted to the painting mechanism, while in JavaFX they can be used in more generic ways. And you can use a Text clipping, giving the node shape by the text.

What are binding properties used for in JavaFX?

JavaFX properties are often used in conjunction with binding, a powerful mechanism for expressing direct relationships between variables. When objects participate in bindings, changes made to one object will automatically be reflected in another object. This can be useful in a variety of applications.


1 Answers

If you have an int you can create a SimpleIntegerProperty from it and then use the asString() on it :

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

If you have an IntegerProperty, you can directly use it

label.textProperty().bind(integerProperty.asString());
like image 199
ItachiUchiha Avatar answered Oct 03 '22 11:10

ItachiUchiha