How to maintain GridPane's fixed-size after adding elemnts dynamically

Posted by Eviatar G. on Stack Overflow See other posts from Stack Overflow or by Eviatar G.
Published on 2014-08-20T11:13:28Z Indexed on 2014/08/21 4:20 UTC
Read the original article Hit count: 144

Filed under:
|
|
|

I need to create board game that can be dynamically change.
Its size can be 5x5, 6x6, 7x7 or 8x8.

I am jusing JavaFX with NetBeans and Scene builder for the GUI.

When the user choose board size greater than 5x5 this is what happens:
enter image description here

This is the template on the scene builder before adding cells dynamically:
enter image description here

To every cell in the GridPane I am adding StackPane + label of the cell number:

@FXML
GridPane boardGame;

public void CreateBoard()
    {
       int boardSize = m_Engine.GetBoard().GetBoardSize();
       int num = boardSize * boardSize;
       int maxColumns = m_Engine.GetNumOfCols();
       int maxRows = m_Engine.GetNumOfRows();

       for(int row = 0; row < maxRows ; row++)
       {
             for(int col = maxColumns - 1; col >= 0 ; col--)
             {
                StackPane stackPane = new StackPane();
                stackPane.setPrefSize(150.0, 200.0);
                stackPane.getChildren().add(new Label(String.valueOf(num)));
                boardGame.add(stackPane, col, row);
                num--;
            }
       }
       boardGame.setGridLinesVisible(true);
       boardGame.autosize();
    }

The problem is the stack panes's size on the GridPane are getting smaller.
I tried to set them equal minimum and maximum size but it didn't help they are still getting smaller.

I searched on the web but didn't realy find same problem as mine.
The only similar problem to mine was found here:
Dynamically add elements to a fixed-size GridPane in JavaFX

But his suggestion is to use TilePane and I need to use GridPane because this is a board game and it more easier to use GridPane when I need to do tasks such as getting to cell on row = 1 and column = 2 for example.

EDIT:
I removed the GridPane from the FXML and created it manually on the Controller but now it print a blank board:

@FXML
GridPane boardGame;
public void CreateBoard()
    {
       int boardSize = m_Engine.GetBoard().GetBoardSize();
       int num = boardSize * boardSize;
       int maxColumns = m_Engine.GetNumOfCols();
       int maxRows = m_Engine.GetNumOfRows();

       boardGame = new GridPane();
       boardGame.setAlignment(Pos.CENTER);
       Collection<StackPane> stackPanes = new ArrayList<StackPane>();
       for(int row = 0; row < maxRows ; row++)
       {
             for(int col = maxColumns - 1; col >= 0 ; col--)
             {
                StackPane stackPane = new StackPane();
                stackPane.setPrefSize(150.0, 200.0);
                stackPane.getChildren().add(new Label(String.valueOf(num)));
                boardGame.add(stackPane, col, row);

                stackPanes.add(stackPane);
                num--;
            }
       }
       this.buildGridPane(boardSize);
       boardGame.setGridLinesVisible(true);
       boardGame.autosize();

       boardGamePane.getChildren().addAll(stackPanes);
    }

    public void buildGridPane(int i_NumOfRowsAndColumns)
    {
        RowConstraints rowConstraint;
        ColumnConstraints columnConstraint;

        for(int index = 0 ; index < i_NumOfRowsAndColumns; index++)
        {
            rowConstraint = new RowConstraints(3, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, VPos.CENTER, true);
            boardGame.getRowConstraints().add(rowConstraint);
            columnConstraint = new ColumnConstraints(3, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, HPos.CENTER, true);
            boardGame.getColumnConstraints().add(columnConstraint);
        }
    }

© Stack Overflow or respective owner

Related posts about java

Related posts about java-ee