JavaFX - creating GridPane takes up all free space in BorderPane

I am using the following code to create a simple grid of buttons for a calculator:

    BorderPane rootNode = new BorderPane();

    GridPane gridPane = new GridPane();
    rootNode.setCenter(gridPane);

    int counter = 1;
    for(int row = 3; row > 0; row--)
        for(int col = 0; col < 3; col++) {
            Button button = new Button("" + counter++);
            button.setOnAction(this);
            gridPane.add(button, col, row);
        }
    gridPane.add(new Button("R"), 0, 4);
    gridPane.add(new Button("0"), 1, 4);

      

(It is required to post an image of what it looks like here, but I do not have enough reputation points to allow this)

The GridPane ends up tiny and crumpled in the top left corner, but I want it to take up all the free space in the center area of ​​the BorderPane (in which case it would be the whole window). I've tried to combine with a bunch of things like various setSize methods, but you're out of luck.

+3


source to share


2 answers


Yours GridPane

already accepts all the space available to it, i.e. whole Borderpane

. Its GridCells that don't use the space available for the GridPane.

You can use the Hgrow

and options Vgrow

available in the GridPane to make the cells take up all the free space.



Here's an example that uses the setHgrow

full width available for the GridPane. You can add the same for Height.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        BorderPane rootNode = new BorderPane();
        rootNode.setMinSize(300, 300);
        rootNode.setStyle("-fx-background-color: RED;");
        GridPane gridPane = new GridPane();
        gridPane.setStyle("-fx-background-color: BLUE;");
        rootNode.setCenter(gridPane);

        int counter = 1;
        for (int row = 3; row > 0; row--)
            for (int col = 0; col < 3; col++) {
                Button button = new Button("" + counter++);
                gridPane.add(button, col, row);
                GridPane.setHgrow(button, Priority.ALWAYS);
            }
        Button r = new Button("R");
        gridPane.add(r, 0, 4);
        GridPane.setHgrow(r, Priority.ALWAYS);
        Button r0 = new Button("0");
        gridPane.add(r0, 1, 4);
        GridPane.setHgrow(r0, Priority.ALWAYS);
        Scene scene = new Scene(rootNode);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

      

+2


source


Sorry I missed this question.



The buttons are too small, so not taking up all the space they have in the borderpane? The width of the button depends on the size of the text. You just write the number in it, so they are pretty small. Use button.setPrefWidth (Integer.MAX_VALUE) and they use all the width they can get!

0


source







All Articles