-1

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

  • The buttons should be placed in 4x6 grid (meaning 4 columns, 6 rows)
  • Edit the grid and buttons in a way that allows the grid to fill the whole window (even when resizing)

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

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Himu21
  • 88
  • 1
  • 8
  • 1
    Create and post a [mre]. – James_D Jan 25 '22 at 18:00
  • And if you're expecting the buttons to grow, why are you setting their max width and height? – James_D Jan 25 '22 at 18:18
  • Well, at first I tried to use `b.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE)` inside of for loop and also inside of getSOSButton() However that function wasn't responding. Then I used Max Min to check if that work or not (this func working) – Himu21 Jan 25 '22 at 18:25
  • @James_D Now I edited that part – Himu21 Jan 25 '22 at 18:28
  • It looks like your update incorporates the pane shown [here](https://stackoverflow.com/q/70841992/230513). – trashgod Jan 25 '22 at 20:25
  • 1
    There are [tips for resizing button layouts](https://stackoverflow.com/questions/23229149/javafx-automatic-resizing-and-button-padding) including a link to a [resizable button grid](https://gist.github.com/jewelsea/2030464), they may teach you some things but probably don’t exactly solve your problem. – jewelsea Jan 25 '22 at 21:20

1 Answers1

2

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);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I've used Hbox and set Hgrow to create the top border three button and a lable (Cheat, Regret, "Advent Calculator 0.1 Alpha" and About) ``final HBox hbox = new HBox( new Button ("Cheat"), new Button("Regret"), leftSpacer, new Label("Advent Calendar 0.1 Alpha"), rightSpacer, new Button("About") );`` Now If i want to add event handler on Cheat Button How can i call it? Note: Please look at the screenshot I added to this post – Himu21 Jan 25 '22 at 20:15
  • 1
    I'm not sure why you can't add the event handler. Ordinarily, I'd suggest that you [edit] your question to include a [mre] that shows your revised approach, but this seems like a design issue unrelated to the button layout issue. Please don't hesitate to use/cite this example if you open a new question. – trashgod Jan 25 '22 at 20:22