I have a GridPane with an arbitrary number of rows and columns, and fill each cell with an ImageView.
Now, this GridPane is contained in an AnchorPane, so when the Window gets resized the GridPane grows or shrinks fine, but the ImageViews does not change its size.
What do I have to do to make the ImageView take the size of its cell in the GridPane?
I'm a beginner to JavaFX and I had a similar problem. I had an AnchorPane which contained a BorderPane. The center of the BorderPane contained a GridPane (gridPaneMaze). This gridPaneMaze used an array ImageView[7][7] images to fill each cell of my gridpane with a square .png file.
I wanted my images to change with the height of the window. Because I used squares, the width of the column should change automatically.
At first, when I resized my window, my columns were broader than my images and the height of the images were to big for the rows.
After trial and error I found that this did the trick:
IMAGES I had for-loop traverse between the rows and columns and in each cell I set the Imageview with the corresponding image (which is always a square) and used this height-binding (I divided the height of the gridpane by the number of rows):
I presume you could also use the widthProperty and divide by the number of columns. But because most monitors have a smaller height than width, I used the height.
GRIDPANE:
I made my gridPane with FXML but that makes no difference to the values.
BORDERPANE: I added a borderpane to my anchorpane, so I set all anchors to '0'.
GRIDPANE: had minWidth 0 and minHeight 0. I set no prefWidth, prefHeight, maxWidth or maxHeight.
ROWS: I set nothing but the vertical alignment to center (no minHeight, prefHeight of maxHeight)
COLUMNS: I set nothing but the horizontal alignment to center (no minWidth, prefWidth of maxWidth)
Hopefully this helps...
This may have two reasons.
1) you might have not defined row and column constraints for your grid pane. Even when a
GridPane
grows, it's cells will not necessarily go beyond their preferred size. The best way to achieve this is to define constraints with a percentage width/height, like so:This will create a 5x2 grid of
ImageView
s wich will fill the entire space of the grid pane equally.2) The solution above will grow your image views, but the
ImageView
class itself will not stretch an image beyond its normal size until told to. You'll probably have to bindImageView#fitWidthProperty
andImageView#fitHeightProperty
as well if you want the images to scale up when the grid expands.