title: Chessboard with automatic resizing
body: <p>So, I am trying to display a chessboard in javaFX. I will have to perform different operations and draw on some of the tiles so I chose to use a Canvas for each tile and a GridPane to arrange them for me in a grid fashion.<br>
  Unfortunately I am having some problems with the resizing of the grid tiles; I want my whole chessboard to automatically adapt its size to the Scene. Therefore, I have added a ChangeListener to both the height and width properties of the GridPane which takes care of resizing the tiles. This only works when the window gets bigger, when the window is reduced to a smaller size everything still gets bigger!<br>
  Here's the shortest <a href="http://sscce.org/" rel="nofollow">SSCCE</a> I came up with which reproduces my problem:</p>
  
  <pre><code>package chessboardtest;
  
  import javafx.application.Application;
  import javafx.beans.value.*;
  import javafx.geometry.*;
  import javafx.scene.*;
  import javafx.scene.canvas.*;
  import javafx.scene.control.*;
  import javafx.scene.layout.*;
  import javafx.scene.paint.*;
  import javafx.stage.Stage;
  
  public class ChessboardTest extends Application {
  
      final int size = 10;
  
      @Override
      public void start(Stage primaryStage) {
          VBox root = new VBox();
  
          final GridPane chessboard = new GridPane();
          fillChessboard(chessboard, size);
  
          ChangeListener&lt;Number&gt; resizeListener = new ChangeListener&lt;Number&gt;() {
  
              @Override
              public void changed(ObservableValue&lt;? extends Number&gt; ov, Number t, Number t1) {
                  double newWidth = chessboard.getWidth() / size;
                  double newHeight = chessboard.getHeight() / size;
  
                  for(Node n: chessboard.getChildren()) {
                      Canvas canvas = (Canvas)n;
                      canvas.setWidth(newWidth);
                      canvas.setHeight(newHeight);
                  }
              }
          };
  
          chessboard.widthProperty().addListener(resizeListener);
          chessboard.heightProperty().addListener(resizeListener);
  
          root.getChildren().add(chessboard);
          root.setPadding(new Insets(10));
          VBox.setVgrow(chessboard, Priority.ALWAYS);
  
          Scene scene = new Scene(root, 300, 250);
  
          primaryStage.setTitle("chessboard");
          primaryStage.setScene(scene);
          primaryStage.show();
      }
  
      void fillChessboard(GridPane pane, int size) {
          class RedrawListener implements ChangeListener&lt;Number&gt; {
              Color color;
              Canvas canvas;
              public RedrawListener(Canvas c, int i) {
                  if(i % 2 == 0) {
                      color = Color.BLACK;
                  }
                  else {
                      color = Color.WHITE;
                  }
                  canvas = c;
              }
  
              @Override
              public void changed(ObservableValue&lt;? extends Number&gt; ov, Number t, Number t1) {
                  canvas.getGraphicsContext2D().setFill(color);
                  canvas.getGraphicsContext2D().fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
              }
          }
  
          for(int row = 0; row &lt; size; row++) {
              for(int col = 0, i = row; col &lt; size; col++, i++) {
                  Canvas c = new Canvas();
                  RedrawListener rl = new RedrawListener(c, i);
                  c.widthProperty().addListener(rl);
                  c.heightProperty().addListener(rl);
  
                  pane.add(c, row, col);
              }
          }
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>