0

Is there a way to make it so the objects/cells of a GridPane are made to fit the parent (TabPane in this instance)? I have been trying this using buttons inside of the GridPane for the past for hours and haven't been able to find a solution.

Here is what I've been trying:

GridPane gPane = new GridPane();
double size = Math.sqrt((tabPane.getTabs().get(0).getContent().getLayoutBounds().getHeight() * tabPane.getTabs().get(0).getContent().getLayoutBounds().getWidth()) / (rows * columns));
for (int i = 0; i < height; i++)
            for (int j = 0; j < width; j++) {
                array[i][j] = new Button();
                array[i][j].setPrefWidth(size);
                array[i][j].setPrefHeight(size);
                gPane.add(array[i][j], i, j);
            }

When I run this, the size of the cells either don't match the size, or when they do, they do not fit the screen as they should.

sorifiend
  • 5,927
  • 1
  • 28
  • 45
Ahxius
  • 19
  • 5

1 Answers1

2

This would be a perfect scenario for using RowConstraints and ColumnConstraints to ensure your objects don't resize the gridpane.

From the Javadoc:

By default, rows and columns will be sized to fit their content; a column will be wide enough to accommodate the widest child, a row tall enough to fit the tallest child.However, if an application needs to explicitly control the size of rows or columns, it may do so by adding RowConstraints and ColumnConstraints objects to specify those metrics. For example, to create a grid with two fixed-width columns:

     GridPane gridpane = new GridPane();
     gridpane.getColumnConstraints().add(new ColumnConstraints(100)); // column 0 is 100 wide
     gridpane.getColumnConstraints().add(new ColumnConstraints(200)); // column 1 is 200 wide

You can also size by percentage which may suite your needs better:

Alternatively, RowConstraints and ColumnConstraints allow the size to be specified as a percentage of gridpane's available space:

 GridPane gridpane = new GridPane();
 ColumnConstraints column1 = new ColumnConstraints();
 column1.setPercentWidth(50);
 ColumnConstraints column2 = new ColumnConstraints();
 column2.setPercentWidth(50);
 gridpane.getColumnConstraints().addAll(column1, column2); // each get 50% of width

Source: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/GridPane.html

sorifiend
  • 5,927
  • 1
  • 28
  • 45
  • I saw something regarding that on another post. However, I didn't know whether I could use constraints while also continuing to use buttons? – Ahxius Feb 21 '22 at 23:53
  • The constraints don't impact what you can or can`t add to the GridPane, they just ensure that the sizing is correct/consistent. – sorifiend Feb 22 '22 at 00:03
  • 1
    Buttons are nodes, so they will resize according to constraints. By default buttons have their max size set to their preferred size, so if you don’t want that, [set their max size to MAX_VALUE](https://stackoverflow.com/questions/23229149/javafx-automatic-resizing-and-button-padding). – jewelsea Feb 22 '22 at 00:05
  • Perfect, it works great. One minor issue: whenever I try to add cells of large values, in which there are more rows than columns (i.e. 40x10), the cells overflow the screen, if that makes sense. – Ahxius Feb 22 '22 at 00:29
  • Disregard, I found I had to set the buttons' min size to MIN_VALUE as well. – Ahxius Feb 22 '22 at 00:40