Quipex
Quipex

Reputation: 37

JavaFX GridPane in ScrollPane is bigger than expected

While I was messing around with javafx, I've encountered such a thing:
I created a GridPane, wrapped it in a ScrollPane and filled with Buttons, but changed RowConstraints and ColumnConstraints for GridPane beforehand.

The problem is that horizontal scrollbar looks inadequate. I believe that it's a GridPane that fat, but how it happens like that?
This is what i'm getting when running the code.

However vertical scrollbar is perfectly fine.

sample.fxml

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SampleController">
   <children>
      <ScrollPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <content>
            <GridPane fx:id="gridPane" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
            </GridPane>
         </content>
      </ScrollPane>
   </children>
</AnchorPane>

sampleController.java

public class SampleController {

    @FXML public GridPane gridPane;

    @FXML
    public void initialize(){
        RowConstraints rowConstraints = new RowConstraints(10.0, 40, 40);
        ColumnConstraints columnConstraints = new ColumnConstraints(10.0, 40, 40);
        int i=0,j=0;
        for(i=0; i<50; i++){
            gridPane.getRowConstraints().add(i, rowConstraints);
            for(j=0; j<30; j++) {
                gridPane.getColumnConstraints().add(j, columnConstraints);
                Button btn = new Button(Integer.toString(j+1));
                btn.setPrefSize(40, 40);
                gridPane.add(btn, j, i);
            }
        }
    }
}

Dashboard.java

public class sample extends Application{
    public static void main(String[] args){
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

        Scene scene = new Scene(root, 300, 275);

        stage.setTitle("FXML Welcome");
        stage.setScene(scene);
        stage.show();
    }
}

Upvotes: 0

Views: 583

Answers (1)

fabian
fabian

Reputation: 82461

You're adding the column column constraints for every row which means in the end you'll have 50 * 30 = 1500 constraints instead of 30. You'll need to add the column constraints in a loop outside of the row loop. Furthermore assuming the constraint lists are empty before you add your constraints, you do not need to specify the index to insert to, since add also inserts to the end of the List. Please check, if you really need the column constraint created in the fxml, since the code becomes a bit simpler, if you do not need to insert the column constraint as second last element of the list:

for (int i = 0; i < 30; i++) {
    gridPane.getColumnConstraints().add(columnConstraints);
}
for(int i = 0; i < 50; i++){
    gridPane.getRowConstraints().add(rowConstraints);
    for(int j = 0; j < 30; j++) {
        Button btn = new Button(Integer.toString(j + 1));
        btn.setPrefSize(40, 40);
        gridPane.add(btn, j, i);
    }
}

BTW: Note that AnchorPane constraints do not have any effect on nodes that are not children of a AnchorPane; you can safely remove those constraints from the GridPane in the fxml.

Upvotes: 1

Related Questions