the goal is to resize a specific area (which is only relevant - the top left corner area) in a GridPane when resizing the window. The relevant area should always fit the window size. The resize mechanism works fine but the GridPane seems to be much much bigger which smalls down the important area.
Is there a way to cut off unused cells in a GridPane?
Or is there a better solution?
Code and screenshot of the problem below:
for (int row = 0; row <= characters.length - 1; row++) {
for (int column = 0; column <= characters[0].length - 1; column++) {
ClockElement n = new ClockElement(characters[row][column], false);
grid.add(n.getLabel(), column, row);
GridPane.setHalignment(n.getLabel(), HPos.CENTER);
ColumnConstraints c = new ColumnConstraints();
c.setHgrow(Priority.ALWAYS);
//c.setMinWidth(40);
c.setPercentWidth(50); //old 40
grid.getColumnConstraints().add(c);
RowConstraints r = new RowConstraints();
r.setVgrow(Priority.ALWAYS);
//r.setMinHeight(40);
r.setPercentHeight(50);
grid.getRowConstraints().add(r);
arrClockElement[row][column] = n;
}
}
grid.setPadding(new Insets(0, 0, 0, 0));
grid.setPrefSize(580, 680); // Default width and height
grid.setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
grid.setGridLinesVisible(true);
You're adding too many ColumnConstraint
s and RowConstraint
s. You need characters.length
(the number of rows) RowConstraints
, and characters[0].length
(the number of columns) ColumnConstraints
. Instead, you are adding both in the inner loop, so you are adding (characters.length)*(characters[0].length)
of each.
Move the RowConstraints
creation outside of the inner loop, and create a new loop for adding the ColumnConstraints
:
for (int row = 0; row <= characters.length - 1; row++) {
RowConstraints r = new RowConstraints();
r.setVgrow(Priority.ALWAYS);
//r.setMinHeight(40);
r.setPercentHeight(50);
grid.getRowConstraints().add(r);
for (int column = 0; column <= characters[0].length - 1; column++) {
ClockElement n = new ClockElement(characters[row][column], false);
grid.add(n.getLabel(), column, row);
GridPane.setHalignment(n.getLabel(), HPos.CENTER);
arrClockElement[row][column] = n;
}
}
for (int column = 0 ; column < characters[0].length; column++) {
ColumnConstraints c = new ColumnConstraints();
c.setHgrow(Priority.ALWAYS);
//c.setMinWidth(40);
c.setPercentWidth(50); //old 40
grid.getColumnConstraints().add(c);
}