将int绑定到javaFX中的Label

And*_*ane 1 binding javafx

我正在尝试将int对象绑定到LabeljavaFX中,并且我不想将模型中的类型更改为IntegerProperty。我尝试了

mainActionLabel.setText(myintvar);

mainActionLabel.textProperty().bind(new SimpleIntegerProperty(myintvar.asString());

但是该值仅在我关闭并再次打开gui时更新,所以我猜绑定实际上并没有起作用,因为我想象它会使用setText方法进行更新。

还有另一种方法可以正确绑定它吗?

编辑:我只是试图删除

mainActionLabel.setText(myintvar);

行,但问题仍然像以前一样存在:已正确初始化,但不能实时更新。仅当我关闭窗口并重新打开它时。

fab*_*ian 5

使用asStringIntegerProperty

IntegerProperty property = new SimpleIntegerProperty();
Button btn = new Button("increment");
btn.setOnAction((ActionEvent event) -> {
    property.set(property.get()+1);
});
Label label = new Label();

label.textProperty().bind(property.asString());
Run Code Online (Sandbox Code Playgroud)