Home > Enterprise >  Imageview changes when Image changes
Imageview changes when Image changes

Time:01-06

I would like to somehow attach imageview to another image and make it so that when this image changes the imageview changes too. I have a "home" controller with an avatar imageview

    user = ViewFactory.getInstance().getUser();

    if(user.getAvatar() != null) {
        imgPreview.setImage(user.getAvatar());
    }

and another "settings" controller which allows to change avatar

    ViewFactory.getInstance().getUser().setAvatar(new Image(avatar.getAbsolutePath()));

and I would like to make it so that when I change avatar in settings it also changes it in home, a kind of listener for home imageview that changes when the value in user.getAvatar() is changed, but i dont know how to archive it

CodePudding user response:

Make the avatar in your User model object an ObjectProperty<Image> (adding appropriate get/set and property accessors);

ObjectProperty<Image> avatar;

Bind the image property in the ImageView to it.

imageView.imageProperty().bind(user.avatarProperty());   

Then, when you set the avatar image property to a new value,

user.setAvatar(newImage);

the image displayed in the image view will also change.

  • Related