title: Javafx adding event handlers in a loop
body: <p>I'm trying to make a method to add SetOnMousePressed-functions to multiple nodes, and i've tried using a couple different loops etc, but i always end up with the error "Local variable x defined in an enclosing scope must be final or effectively final." This is as far as i've gotten:</p>
  
  <pre><code>public static int playerSelectingCategory(int intScorecard[][], Rectangle[][] scoreboardBackground, int categoryCounter, int nrOfPlayers, boolean limitCheck)
  
      {
      int counter = 0;
      int y = 0;
  
      for(int x = 0; x&lt;YatzyConstants.getNrCategories(); x++)
      {
      if(counter &lt; nrOfPlayers)
      {
          if(y &lt; YatzyConstants.getNrCategories())
          { 
              scoreboardBackground[counter][y].setOnMousePressed(e -&gt;
              {
                      scoreboardBackground[counter][y].setFill(javafx.scene.paint.Color.ALICEBLUE);   
              });
            y++; 
          }
          counter++;
  
      }}
      return intScorecard[counter][y];
  }
  </code></pre>
  
  <p>I originally declared everything one by one, but i have to think that there should be a more effective way of doing it. Any help is appreciated, really hit a brick wall here. </p>