1

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);
        }
    });
}

}
Hernan Romero
  • 133
  • 10
  • I can solve your problem if you share your code and help me understand what is missing in your code. – Ugurcan Yildirim Sep 14 '15 at 18:47
  • 1
    Your code works just fine for me; it resizes as expected. So your errors are most likely elsewhere in your code. Please create and post a [MCVE]. – James_D Sep 14 '15 at 20:03

0 Answers0