javajavafxgridpane

Resizable Buttons in 4*6 GridPane


My target is to build a Window in JavaFX where there will be 24 buttons, such way that:

The problem I've right now is regarding the button size, I was able to increase the button size using setMinHeight and setMaxHeight However It's not responsive incase of window size change.

My code-

   private Button getSOSButton(){
    //create button

    Button b = new Button();
     b.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE)
    return b;
};

I've tried b.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE) inside of the loop but it's not working.

    int i=0;
    int j=0;
    GridPane gridPane = new GridPane();
    for(i=0;i<6;i++) {
        for (j = 0; j < 4; j++) {
            gridPane.add(getSOSButton(), i, j, 1, 1);
        }
    }
    gridPane.setPadding(new Insets(25,0,0,0));
    gridPane.setAlignment(Pos.CENTER);

        

    StackPane layout = new StackPane();
    layout.getChildren().addAll(hbox,gridPane);
    Scene scene = new Scene(layout, 600, 800);
    stage.setScene(scene);
    stage.show();

This is my expected output - This is my expected output

This is my code result - This is my code result


Solution

  • I altered the second example examined here to make the button grow; resize the stage to see the effect. Noting the GridPane optional layout constraints, I allowed each Button to always grow arbitrarily in the center of its grid cell.

    button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
    …
    GridPane.setHalignment(gb, HPos.CENTER);
    GridPane.setHgrow(gb, Priority.ALWAYS);
    GridPane.setValignment(gb, VPos.CENTER);
    GridPane.setVgrow(gb, Priority.ALWAYS);
    

    image

    import java.util.ArrayList;
    import java.util.List;
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.geometry.HPos;
    import javafx.geometry.Insets;
    import javafx.geometry.VPos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.Priority;
    import javafx.stage.Stage;
    
    /** @see https://stackoverflow.com/a/69429741/230513 */
    public class GridButtonTest extends Application {
    
        private static final int N = 5;
        private final List<List<Button>> list = new ArrayList<>();
    
        private Button getGridButton(int r, int c) {
            return list.get(r).get(c);
        }
    
        private Button createGridButton(int row, int col) {
            Button button = new Button("r" + row + ",c" + col);
            button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
            button.setOnAction((ActionEvent event) -> {
                System.out.println(event.getSource() == getGridButton(row, col));
            });
            return button;
        }
    
        @Override
        public void start(Stage stage) {
            stage.setTitle("GridButtonTest");
            GridPane root = new GridPane();
            for (int row = 0; row < N - 1; row++) {
                list.add(new ArrayList<>());
                for (int col = 0; col < N + 1; col++) {
                    Button gb = createGridButton(row, col);
                    list.get(row).add(gb);
                    root.add(gb, row, col);
                    //GridPane.setMargin(gb, new Insets(N));
                    GridPane.setHalignment(gb, HPos.CENTER);
                    GridPane.setHgrow(gb, Priority.ALWAYS);
                    GridPane.setValignment(gb, VPos.CENTER);
                    GridPane.setVgrow(gb, Priority.ALWAYS);
                }
            }
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }