title: Generate A Big GridPane with Java FXML
body: <p>Hi I'm a newbie in FXML and in general in JavaFX.</p>
  
  <p>As said in the title, I want to create a GridPane with 64+ cells.
  My question is: </p>
  
  <p>Do I have to write all the code for those 64+ cells or there is a way (like a for loop) to generate them?</p>
  
  <p>Here is my code. My plan is to have a grid of 8x8 (and maybe more) Grass cells.</p>
  
  <p>Grass Class</p>
  
  <pre><code>package auto;
  import javafx.scene.paint.Color;
  import javafx.scene.shape.*;
  
  public class Grass extends Ground{
  
      public Grass(){
          ground= new Rectangle(50,50,Color.GREEN);
      }   
  }
  </code></pre>
  
  <p>Ground Class</p>
  
  <pre><code>package auto;
  
  import javafx.scene.shape.Rectangle;
  
  
  public abstract class Ground {
      protected Rectangle ground;
  }
  </code></pre>
  
  <p>FXML Document</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import java.lang.*?&gt;
  &lt;?import java.util.*?&gt;
  &lt;?import javafx.scene.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.shape.*?&gt;
  &lt;?import javafx.geometry.*?&gt;
  
  &lt;StackPane   alignment="CENTER"
     xmlns:fx="http://javafx.com/fxml"
     fx:controller="auto.FXMLDocumentController"
  &gt; 
       &lt;Rectangle fx:id="Back" id="Back" height="700" width="500" /&gt;
       &lt;VBox id="APP" prefWidth="450" maxWidth="450" minWidth="450"  &gt;
            &lt;StackPane style="-fx-border-color: green; -fx-border-width: 2px;"&gt;
                &lt;Rectangle id="menu-style" height="50" width="450"/&gt;
  
                &lt;HBox id="Menu"&gt; 
                   &lt;Button text="Start"/&gt;
                   &lt;Label&gt; Current Cars&lt;/Label&gt;
                   &lt;Button text="Add Car"/&gt;  
                &lt;/HBox&gt;
  
            &lt;/StackPane&gt;
           &lt;GridPane id="Game" fx:id="Game"&gt;
           //8x8 grid of Grass 
  
           &lt;/GridPane&gt;
       &lt;/VBox&gt; 
  &lt;/StackPane&gt;
  </code></pre>
  
  <p>Main Java code</p>
  
  <pre><code>package auto;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.layout.Pane;
  import javafx.stage.Stage;
  
  
  public class RaceGame extends Application {
  
      @Override
      public void start( Stage stage ) throws Exception {
  
         stage.setTitle("Race Game");
         Parent root =FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
         String css = this.getClass().getResource("RaceGame.css").toExternalForm();
         Scene scene = new Scene(root);
         scene.getStylesheets().add(css);
         stage.setScene(scene);
         stage.show();
      }
  
      /**
       * @param args the command line arguments
       */
      public static void main( String[] args ) {
          launch(args);
      }
  
  }
  </code></pre>
  
  <p>EDIT#1</p>
  
  <p>I tried using the controller. The problem is that Grass is not a Node.</p>
  
  <pre><code>package auto;
  
  
  import javafx.fxml.FXML;
  import javafx.scene.layout.GridPane;
  
  public class FXMLDocumentController  {
  
      @FXML
      private GridPane Game;
  
      public FXMLDocumentController() {
          for(int i=0;i&lt;8;i++){
             for(int j=0;j&lt;8;j++){
                 Game.add(new Grass(), i, j);
             }
          }
  
      }
  
  }
  </code></pre>