JavaFX将图像属性绑定到图像(MVC)



我在寻找答案时偶然发现了这个问题。但这似乎并不能解决我的案子。

在我的视图控制器中,我有以下内容:

public void setModel(CarcassonneModel model) {
   this.model = model;
   ivHoveringTile.imageProperty().bind(getImage(model.board.getActiveTile().getFilename()));
}
private ObjectProperty<Image> getImage(String filename) {
        File file = new File("src/carcassonneapplicatie/resources/tiles/" + filename + ".png");
        Image image = new Image(file.toURI().toString());
        ObjectProperty<Image> imageProperty = new SimpleObjectProperty<>(image);
        return imageProperty;
    }

但是,当我使用操作事件更改模型中的文件名时,显示的图像不会改变。我的标签还有其他绑定,除了这一个,它们似乎很完美。

如果进行

someProperty.bind(someOtherProperty);

则每当调用CCD_ 2时CCD_。

在您的代码中,someOtherProperty是您在getImage()方法中创建的ObjectProperty<Image>。由于您甚至没有保留对此属性的引用,因此不可能对其调用set(...)。因此ivHoveringTile中的图像永远不会更新。

您需要绑定到模型中的一个可观察对象,表示可能更改的实际值。

最新更新