title: Dynamically change the color of a Rectangle in Javafx
body: <p>I am creating a two <code>javafx.scene.shape.Rectangle</code> objects in a <code>GridPane</code> and doing the following.</p>
  
  <pre><code>rectArray = new Rectangle[2];
  
  boardGrid.setStyle("-fx-background-color: #C0C0C0;");
  
  rectArray[0] = new Rectangle(12,12);
  rectArray[0].setFill(Color.AQUA);
  boardGrid.add(rectArray[0], 2, 0);
  
  rectArray[1] = new Rectangle(12,12);
  rectArray[1].setFill(Color.BLACK);
  boardGrid.add(rectArray[1], 2, 1);       
  
  Button buttonStart = new Button("Change color");
  buttonStart.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler&lt;MouseEvent&gt;() {
         @Override
         public void handle(MouseEvent event) {
            rectArray[0].setFill(Color.RED);
            try {
                 Thread.sleep(2000);
            } 
            catch (InterruptedException e) {
                 e.printStackTrace();
            }
            rectArray[1].setFill(Color.AZURE);
         }
  });
  boardGrid.add(buttonStart, 3, 1);
  initializeScene(primaryStage, boardGrid);
  ...
  </code></pre>
  
  <p>When I run the code I am able to see two rectangles (One in Aqua and one in black) and when I click the button, I will have to wait for the 2 seconds to view the change in colors of both rectangles. </p>
  
  <p>I change the color of one rectangle before <code>Thread.sleep(2000)</code> is called and then I change the color of the next rectangle.  </p>
  
  <p>My question is why am I supposed to wait for 2 seconds? Is there a way that I can dynamically update the colors of the rectangle?</p>