Clean gridPane in JavaFX and maintain the Grid line
Asked Answered
L

5

12

I'm trying to make a simple game (it is a school work) in JavaFX and I am trying to clear the panel with the board, and then just repaint it. I have tried a lot of methods, and this one is the only one I found that removes all the pieces of the board (visually) without creating a visual bug that shows a piece that has already been deleted but is still shown.

So I declare the gridPane like this:

private GridPane gridPecas;

@Override
public void start(Stage primaryStage)
{
    gridPecas = new GridPane();
    gridPecas.setGridLinesVisible(true);

    paintBoard();

    // rest of the code to create and paint the Stage
 }

 private void paintBoard()
 {
    gridPecas.getChildren().clear();
    // Code to fill the board with ImageView and Images of the pieces
 }

The problem with this method, is that when the "gridPecas.getChildren().clear();" is called I just loose the grid lines from the GridPanel.

How can I solve this problem?

Laquitalar answered 21/6, 2012 at 22:32 Comment(0)
E
9

setGridLinesVisible(boolean) is for debug only:

http://docs.oracle.com/javafx/2/api/javafx/scene/layout/GridPane.html#setGridLinesVisible(boolean)

You can't even change the gridLines color for exemple.

So you should find another way to make your gridlines, and then call

yourPane.getChildren().clear();

because it's better than gridPecas.getChildren().removeAll(pecasFX[i][j]);

Elexa answered 20/8, 2013 at 10:13 Comment(0)
D
4

A way to clear the GridPane and maintain the Gridlines is as follow :

    Node node = grid.getChildren().get(0);
    grid.getChildren().clear();
    grid.getChildren().add(0,node);

the first Node* in the GridPane (*if you set the GridLinesVisible before adding elements in the grid?) contains the Gridlines (and maybe other things).

So you just need to keep this Node before the clear & re-add it after

Downward answered 9/4, 2015 at 10:25 Comment(0)
O
1

a one-liner: grid.getChildren().retainAll(grid.getChildren().get(0));

Oxtail answered 12/6, 2016 at 4:31 Comment(0)
S
1

Valid clear GridPane and restore mainrtain grid lines:

gridPane.setGridLinesVisible(false);
gridPane.getColumnConstraints().clear();
gridPane.getRowConstraints().clear();
gridPane.getChildren().clear();
gridPane.setGridLinesVisible(true);
Shipper answered 19/2, 2020 at 10:1 Comment(0)
S
0

Hmm my suggestion is not to clear all children at all. Always leave the imageViews and just replace the images they show. To "clear" the board put an "empty" image.

Succeed answered 22/6, 2012 at 7:54 Comment(1)
Thank you for your answers, but I've just found the perfect solution xD Instead of doing "gridPecas.getChildren().clear();" I just do this in the for cycle : "gridPecas.getChildren().removeAll(pecasFX[i][j]);" So that when reading the piece it deletes it from the grid, and, thanks to the original "fill board" code, he hads a new one leaving the border clean and updated.Laquitalar

© 2022 - 2024 — McMap. All rights reserved.