0

I'm trying to make an imageview fit the size of its container (in my case a Pane), and at the same time place it in the center of the Pane in JavaFx, in detail I can't do both at the same time i can get it to fit the container, but not put it in the center.

here is the code i use to make it fit in the container Pane

        //imgContainer is the pane that contains the image
        //photo is my imageview
        photo.fitWidthProperty().bind(imgContainer.widthProperty());
        photo.fitHeightProperty().bind(imgContainer.heightProperty());
        photo.setImage(new Image(post.getPhoto()));

this is the output that is produced, the photo adapts to the Pane dimensions, but it is located to the left of the container(the dark background is the Pane container) would to put it in the center of the pane and i couldn't find a method to do it keeping the dimensions of the Pane for the imageview, putting it on a hbox or stackPane allows to put it in the center but the image no longer keeps the container sizes well, so i don't know how to get it centered and fits the container at the same time

felix
  • 13
  • 1
  • [This](https://stackoverflow.com/a/74403844/2189127) is more complicated, because it handles three separate images at the same time, but if you understand what’s it’s doing you should be able to simplify it to scale and center a single image. – James_D Jan 01 '23 at 21:45
  • 3
    Also, have you tried just wrapping your `photo` in a `StackPane`, which centers its content by default? Though I generally prefer the approach linked above. – James_D Jan 01 '23 at 21:47
  • This [alternative](https://stackoverflow.com/a/73328643/230513) starts centered and offers basic pan/zoom. – trashgod Jan 02 '23 at 00:48
  • The [css settings for background images](https://openjfx.io/javadoc/19/javafx.graphics/javafx/scene/doc-files/cssref.html#region) provide a lot of flexibility. – jewelsea Jan 02 '23 at 12:27

0 Answers0