title: JavaFX - How to assign an Event Handle to an Array of Labels?
body: <p>I am using Eclipse and Java (JavaFX)</p>
  
  <p>I have a GridPane and an Array with Labels. I added to each row and column of the GridPane a Label.</p>
  
  <pre><code>GridPane grid = new GridPane();
  Label lbs[] = new Label[20*20];
  </code></pre>
  
  <p>When i hover over a label, i want something to happend. Lets say the background should change.</p>
  
  <p>If I had a single label without an array, it would work like this:</p>
  
  <pre><code>lbs.setOnMouseEntered(new EventHandler&lt;MouseEvent&gt;() {
            @Override
            public void handle(MouseEvent e) {
                lbs.setStyle("-fx-background-color:BLACK;");
  
            }
          });
  </code></pre>
  
  <p>But since its an Array of Labels, it underlines the whole code block above in red and gives me this error message: 
  <strong>Cannot invoke setOnMouseEntered(new EventHandler(){}) on the array type Label[]</strong></p>
  
  <p>The only way it work is:</p>
  
  <pre><code>lbs[1].setOnMouseEntered(new EventHandler&lt;MouseEvent&gt;() {
       @Override
       public void handle(MouseEvent e) {
        lbs[1].setStyle("-fx-background-color:BLACK;");
  
       }
     });
  </code></pre>
  
  <p>But I dnt wanna do it for all Labels. There should be a easier way i hope.</p>
  
  <p>What should I do to make it work? I am new in this forum. My Prof suggested this to me and therefore I hope I added the codes here in a proper way.</p>
  
  <p>Here is my whole code:</p>
  
  <pre><code>    package application;
  
  import javafx.application.Application;
  import javafx.stage.Stage;
  import javafx.scene.Scene;
  import javafx.scene.control.Label;
  import javafx.scene.layout.ColumnConstraints;
  import javafx.scene.layout.GridPane;
  import javafx.scene.layout.RowConstraints;
  
  
  public class Main extends Application {
  @Override
  public void start(Stage primaryStage) {
      try {
          GridPane grid = new GridPane();
  
           Scene scene = new Scene(grid, (20 * 20), (20 * 20));
          scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
          primaryStage.setScene(scene);
          primaryStage.show();    
          Label lbs[] = new Label[20*20];
  
  
          grid.setGridLinesVisible(true);
  
           for (int i = 0; i &lt; lbs.length; i++) {
                  lbs[i]=new Label();
           }
  
           int count = 0;
           for (int x = 0; x &lt; lbs.length/20; x++){
                  for (int y = 0; y &lt; lbs.length/20; y++){
                      grid.add(lbs[count], x, y);
                      count++;
                  }
              }
  
  for(int i = 0; i &lt; 20; i++) {
  ColumnConstraints column = new ColumnConstraints(20);
  grid.getColumnConstraints().add(column);
  
  }
  
  for(int i = 0; i &lt; 20; i++) {
  RowConstraints row = new RowConstraints(20);
  grid.getRowConstraints().add(row);
  }
  
      } catch(Exception e) {
          e.printStackTrace();
      }
  }
  
  public static void main(String[] args) {
      launch(args);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here my CSS file, just in case it is important help solving this problem:</p>
  
  <pre><code>GridPane {
      -fx-background-color: white ;
  
  
      -fx-grid-lines-visible: true;
      -fx-border-color: black 
  }
  
      Label {
            -fx-pref-height: 20;
                -fx-pref-width: 20;
          -fx-background-color: green ;
          -fx-font-size: 11pt;
          -fx-font-family: "Segoe UI Semibold";
          -fx-text-fill: Black;
          -fx-opacity: 0.6;
  
      }
  </code></pre>