Bashfire
Bashfire

Reputation: 41

Javafx resize ImageView to AnchorPane

I have an ImageView inside an AnchorPane and I want to resize the Image in the same proposition as the AnchorPane resize.

Can someone explain me how to do that?

Many thanks in advance.

Upvotes: 3

Views: 2127

Answers (3)

Dexter Legaspi
Dexter Legaspi

Reputation: 3312

See this answer. Basically you can use the ImageViewPane that you can include in your project.

Upvotes: 0

Anthone
Anthone

Reputation: 2290

Only thing work for me. On the view, listen change of the AnchorPane and set ImageView Size

init {
   myAnchorPane.widthProperty().addListener { observable, oldValue, newValue ->
            myImageView.fitWidth = myAnchorPane.width
            myImageView.fitHeight = 0.0
    }
}

Upvotes: 0

James_D
James_D

Reputation: 209724

You can bind the image view's fitWidth and fitHeight properties to the width and height of the anchor pane:

imageView.fitWidthProperty().bind(anchorPane.widthProperty());
imageView.fitHeightProperty().bind(anchorPane.heightProperty());

If you also want to preserve the proportions of the image you are displaying, you can do

imageView.setPreserveRatio(true);

Upvotes: 3

Related Questions