Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javafx Bindings.createStringBinding but binding not actually work

Tags:

binding

javafx

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;}
}    
like image 819
orioncs Avatar asked Sep 14 '25 09:09

orioncs


1 Answers

You need to "tell" Bindings, which Observables 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()));
like image 128
fabian Avatar answered Sep 16 '25 14:09

fabian