I'm trying to bind the textProperty
of the Label
to the object's SimpleIntegerProperty
with help of Bindings
but it does not change the text when I change the SimpleIntegerProperty
of the object in real time. Any help would be appreciated of how to make textProperty
change.
package sample;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable
{
@FXML
private Slider slider;
@FXML
private Label label;
@Override
public void initialize(URL location, ResourceBundle resources) {
MyObject object = new MyObject(0);
label.textProperty().bind(Bindings.createStringBinding(() -> " hello " + object.numberProperty().get() * (10 + 12)/2));
object.numberProperty().bind(slider.valueProperty());
}
}
class MyObject {
private SimpleIntegerProperty number;
public Object(int number){
this.number = new SimpleIntegerProperty(number);
}
public SimpleIntegerProperty numberProperty(){return this.number;}
}
You need to "tell" Bindings
, which Observable
s to observe for changes. This varargs parameter is the second parameter of the createStringBinding
method. In this case you need to pass only a single Observable
: object.numberProperty()
label.textProperty().bind(
Bindings.createStringBinding(
() -> " hello " + object.numberProperty().get() * (10 + 12)/2,
object.numberProperty()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With