JavaFx Resizing imageview to fit the container Pane and set it on the center - javafx

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

Related

Auto-size pane in JavaFX like scene builer have

I creating site builder and I want anchorpane at center of another pane. I have 3 anchor panes in SplitPane and in the central pane I need rezise of object like scene builder have when you resize scene builder. How to make that resizing? Is it very hard? I want to make this in fxml or directly in scene builder. I use IntelliJ.
This picture is show how it look when you open at first time and it need to be always 16:9 (central pane). Maybe you know what pane can be it
And this is how it look when you open in full-screen
In this example I tried to use borderPane and I put empty anchorpane's in all other corners because when you put something in center it fit all borderPane. How I remember when you do this thing in anchorPane with constraints (like 100px from upper corner) it looks the same.
How to do normal 16:9 all-time resizing?

BorderPane center overlap other parts

When I use BorderPane and put a layout in the center part , it covers other parts , how can I prevent the center part of covering other parts n borderPane
as you see , the table covers the right part of the BorderPane .
I put the table inside Pane .
From the BorderPane documentation:
BorderPane does not clip its content by default, so it is possible that childrens' bounds may extend outside its own bounds if a child's min size prevents it from being fit within it space.
To prevent overflow, some options are:
You could code your child node so that it does not have a min size.
You can ensure that the sum of min sizes of all children is at least equal to the min size of the border pane.
You can explicitly set a clip on your child node. For a demo, see: JavaFX Pane Clipping.
final Rectangle outputClip = new Rectangle();
childNode.setClip(outputClip);
childNode.layoutBoundsProperty().addListener((ov, oldValue, newValue) -> {
outputClip.setWidth(newValue.getWidth());
outputClip.setHeight(newValue.getHeight());
});
The order you set child nodes into the border pane determines which is displayed on top, so if all the nodes are opaque, you could set the center pane first and the sides panes will be painted over any overflow.

JavaFX Scenebuilder moving elements inside StackPane

I am trying to move things such as buttons and labels that I put inside a StackPane since I want these things to stay centered when minimizing or maximizing windows. When I put everything I want into the StackPane, they all get centered and layered up on each other. How am I able to move these elements around in the StackPane if it's possible or how will I be able to keep everything centered when resizing the window?
You edit their TranslateX and TranslateY properties. for instance
Label label1 = new Label();
label1.setText("0");
label1.TranslateX(-10.0); //<< moves the label 10 pixels to the left.
if you are using Scene Builder you can edit these properties on the right under the "layout" tab.

Scaled content in JavaFX Scrollpane

I need a scaled ImageView with a Canvas overlay, stretched over the parent, with a constant margin on the right hand side. This suggests an AnchorPane with the ImageView and Canvas as children, each with a rightAnchor inset representing the margin.
I need to put the AnchorPane in a ScrollPane. The ScrollPane must, when resized, resize both the ImageView and the Canvas overlay within the AnchorPane, while retaining the margin.
I have tried adding the ImageView and Canvas to the AnchorPane, and adding the AnchorPane to the ScrollPane, with ScrollPane.setFitToWidth(true). When I increase the size of the ScrollPane, all works well - both the ImageView and Canvas expand to fill the ScrollPane, retaining the margin on the right hand side.
However, when I reduce the size of the ScrollPane, the size of the AnchorPane is not reducing, but instead the viewport over the AnchorPane is reducing, with increasing scrollbars.
NB, an ImageView inserted directly into the ScrollPane does work - it rescales both on grow and shrink.
It looks as though ScrollPane.setFitToWidth behaviour differs depending on the container inside the ScrollPane.
Is this correct, and can anyone suggest a solution, please?
Robert
Try adding a listener to one of the ScrollPane's Bounds properties and reduce the AnchorPane's size from there when appropriate.
Solved using Jurgen's answer.
I overrode AnchorPane.computeMinHeight, computePrefHeight and computeMaxHeight to return imageView.minHeight, prefHeight and maxHeight. This ties the anchor pane's height to the imageView's height - which the imageView calculates from its own width.
Then I added a listener to the scrollPane.widthProperty which calls the AnchorPane.setPrefWidth, setMinWidth and setMaxWidth
In this way, the anchor pane is correctly signalling its preferred, minimum and maximum width to the enclosing scrollPane.

[JavaFX][FXML] Scrollpane filled by titlepanes. TitlePane filled by TilePane. (Changing container size due to changing element inside)

i've got following problems.
How, in FXML, put for ex. 3 TitlePanes into ScrollPane, in such way, that when i open all 3 TitlePanes, the vertical scroll will appear, but when i have for ex. 1 open it disappear? (fit ScrollPane height to it's dynamicaly changing content)
How, in FXML, put TilePane into TitlePane, that TitlePane will fit to it content? For ex. i put 4 Buttons into TilePane. At begining it's 2x2. When i reduce width, it change into 4x1, TilePane heigth grow, but TitlePane doesnt grow with it, and not all Buttons are visible. How to connect TilePane size with its container TitlePane size?
I'll be appriciate for any help.
If you are using FXML, make sure that you do not set the preferred height of the controls you want to resize, they should be set to COMPUTED. With this set, a collapsed TitledPane will only take up the size of it's title.

Resources