javafx Bindings.createStringBinding but binding not actually work

本秂侑毒 提交于 2019-12-20 03:26:04

问题


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;}
}    

回答1:


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()));


来源:https://stackoverflow.com/questions/38543273/javafx-bindings-createstringbinding-but-binding-not-actually-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!