Layout – how do I get the buttons to populate the JavaFX gridpane?

Java swing has a GridLayout that allows you to specify the size of an array of widgets, such as 3x4 The widgets then populate the panels they occupy How do you achieve similar results in JavaFX?

Solution

What I want you to ask is how to get a node in the grid pane to fill the space allocated to its cells

There are several ways to do this You can use the static gridpane method to apply gridpane specific property settings to nodes:

GridPane.setFillWidth(myButton,true);
GridPane.setFillHeight(myButton,true);

Another way is to specify column and row constraints on the grid pane itself:

GridPane grid = new GridPane();
for (int rowIndex = 0; rowIndex < numRows; rowIndex++) {
    RowConstraints rc = new RowConstraints();
    rc.setVgrow(Priority.ALWAYS) ; // allow row to grow
    rc.setFillHeight(true); // ask nodes to fill height for row
    // other settings as needed...
    grid.getRowConstraints().add(rc);
}
for (int colIndex = 0; colIndex < numColumns; colIndex++) {
    ColumnConstraints cc = new ColumnConstraints();
    cc.setHgrow(Priority.ALWAYS) ; // allow column to grow
    cc.setFillWidth(true); // ask nodes to fill space for column
    // other settings as needed...
    grid.getColumnConstraints().add(cc);
}

In either case, you need to grow the nodes so that they comply with the growth requests made by the grid pane So, for example:

Button myButton = new Button("Click");
myButton.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);
grid.add(myButton,0);

Sscce (using row and column constraint methods):

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

public class KeyPad extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane grid = new GridPane();
        int numRows = 4 ;
        int numColumns = 3 ;
        for (int row = 0 ; row < numRows ; row++ ){
            RowConstraints rc = new RowConstraints();
            rc.setFillHeight(true);
            rc.setVgrow(Priority.ALWAYS);
            grid.getRowConstraints().add(rc);
        }
        for (int col = 0 ; col < numColumns; coL++ ) {
            ColumnConstraints cc = new ColumnConstraints();
            cc.setFillWidth(true);
            cc.setHgrow(Priority.ALWAYS);
            grid.getColumnConstraints().add(cc);
        }

        for (int i = 0 ; i < 9 ; i++) {
            Button button = createButton(Integer.toString(i+1));
            grid.add(button,i % 3,i / 3);
        }
        grid.add(createButton("#"),3);
        grid.add(createButton("0"),1,3);
        grid.add(createButton("*"),2,3);

        Scene scene = new Scene(grid);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Button createButton(String text) {
        Button button = new Button(text);
        button.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);
        button.setOnAction(e -> System.out.println(text));
        return button ;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>