I'm starting with Javafx and I have a doubt with automatic resizing. I have an empty grid pane in a window with a few text labels and buttons. The window is sized to match the content. I need to automatically resize the window when adding children to the grid pane, I thought that I would solve it by calling sizeToScene after adding the children but I think it is not working because I am calling it before drawing so it resizes to the height of the empty grid pane (so it does nothing). Is there a simple way to solve this? Thank you!!
public class controller {
@FXML Button button_populate;
@FXML GridPane numbers_pane;
@FXML BorderPane root;
public controller(){
}
private void setNumberTable(int numberOfButtons){
int buttonNumber = 1;
int rows = (int) numberOfButtons/10;
for (int row = 0; row < rows; row++) {
for (int column = 0; column < 10 && buttonNumber <= 100; column++) {
Button button = new Button(String.valueOf(buttonNumber++));
numbers_pane.add(button, column, row);
}
}
Stage stage = (Stage) root.getScene().getWindow();
stage.sizeToScene();
}
@FXML
private void initialize() {
button_populate.setOnAction(new javafx.event.EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
setNumberTable(100);
}
});
}
}