-3

I'm trying to create a Tic-Tac-Toe-like program with a board size that users can select. I can't figure out how to adjust the size of a gridpane in the code. My only lead was using ColumnConstraints and RowContraints, but I have two issues:

  1. When adding a row or column, it won't add multiple rows and columns in a for loop.
  2. I can't figure out how to resize both the grid and the window so that larger version of the grid is usable.

Code for function here:

public void changeGameBoard(ActionEvent event) {
    if (boardNumber > 2) {
        boardNumber = 50;
        sizeLabel.setText("Set at :" + boardNumber);
        ColumnConstraints column1 = new ColumnConstraints();
        RowConstraints row1 = new RowConstraints();
        for (int i = 0; i < boardNumber; i++) {
            column1.setPrefWidth(100);
            row1.setPrefHeight((100));
        }
        gameBoard.getColumnConstraints().add(column1);
        gameBoard.getRowConstraints().add(row1);
        gameBoard.setMinSize(500,500);
    }
}

I've tried playing with padding in SceneBuilder and with setMinSize of my gridpane (the gameBoard object). I can't find any resources to help though. I'm also working with IntelliJ.

HoRn
  • 1,458
  • 5
  • 20
  • 25
user120265
  • 13
  • 3
  • 1
    But you are only adding one `ColumnConstraints` and one `RowConstraints`. – James_D Nov 03 '22 at 19:57
  • I thought you add a single constraint per row or column your adding? Could you explain how I would make use of the constraints? – user120265 Nov 03 '22 at 20:28
  • 1
    Yes, that's what you need to do. You're not doing that. Read your code. – James_D Nov 03 '22 at 20:49
  • That's my problem. I do not understand how to use these methods. How would I create n number of rows and columns? – user120265 Nov 03 '22 at 23:02
  • 2
    In the code you posted `boardSize` is 50, so I assume you want 50 rows and columns. You create one, and only one `RowConstraints` object. Then you set the height of that one `RowConstraints` to 100, but you do that 50 times. And then you add it, once, to the grid pane. As you said earlier, you need to add one `RowConstraints` *per row*, so you need to add 50 of them. – James_D Nov 04 '22 at 00:32
  • When I do that, the rows don't stay evenly spaced and start to group up near one side of the grid. Also, the 50 is a typo. The goal is for user to enter whatever number they want to set grid size. – user120265 Nov 05 '22 at 15:14
  • Probably there is not enough space to accommodate 50 rows/columns at the specified size (you would need a window at least 5000x5000 pixels), and so some of them are being sized below their preferred size. I don’t understand your comment about a “typo”, just change it to use the number input by the user instead. – James_D Nov 05 '22 at 17:10

1 Answers1

-2

The question is a little vague out of context, but assuming "gameBoard" is your grid pane, why not set up your grid pane constraints to be a percentage, something like this.

    GridPane gameBoard = new GridPane();
    ColumnConstraints colThird = new ColumnConstraints();
    colThird.setPercentWidth(1/3);
    gameBoard.getColumnConstraints().addAll(colThird,colThird,colThird);
    RowConstraints rowThird = new RowConstraints();
    rowThird.setPercentHeight(1/3);
    gameBoard.getRowConstraints().addAll(rowThird,rowThird,rowThird);
M. Rogers
  • 367
  • 4
  • 18
  • gameBoard is my grid pane. Thank you for answering my question! I don't know how to adapt that code to my use. I need the Gridpane to vary in number of columns and rows based on user preferences. If a user selects 7 as boardNumber, then there needs to be seven rows and columns that are evenly spaced. – user120265 Nov 05 '22 at 15:25
  • 1
    Note that `1/3` won’t work here, since it evaluates to `0`. You would need `1.0/3.0`. – James_D Nov 05 '22 at 17:13
  • change the 1/3 to a 1/7, and in the "addAll", add it 7 times instead of 3. The smart way to do it is set it up as a variable instead of hard coding it. But as @James_D Pointed out, make sure its a double. 1.0 and 3.0 will work. – M. Rogers Nov 05 '22 at 23:33