Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean gridPane in JavaFX and maintain the Grid line

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?

like image 510
aliasbody Avatar asked Jun 21 '12 22:06

aliasbody


People also ask

How do you clear a GridPane?

A way to clear the GridPane and maintain the Gridlines is as follow : Node node = grid. getChildren(). get(0); grid.

How does GridPane work in JavaFX?

GridPane lays out its children within a flexible grid of rows and columns. If a border and/or padding is set, then its content will be layed out within those insets. A child may be placed anywhere within the grid and may span multiple rows/columns.

How do I delete a column in GridPane?

To remove nodes, just use gridpane. getChildren(). remove(...); or gridpane. getChildren().


1 Answers

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]);

like image 101
Flo C Avatar answered Sep 19 '22 16:09

Flo C