title: JavaFX: Create EventHandler
body: <p>I'm writting an event handler for a button, on the net I saw a lot of codes like this:</p>
  
  <pre><code>b = new Button("click me");
      b.setOnMouseClicked(new EventHandler&lt;MouseEvent&gt;() {
  
          @Override
          public void handle(MouseEvent t) {
          /*
              calling several oblects' methods and doing things
          */
          }
      });
  </code></pre>
  
  <p>If I have a method like <code>setGUI</code> and I must create several buttons and several <code>eventhandlers</code>, the question is: is it ok to put a lot of "action" there? If I have 10 <code>buttons</code> and every button has an <code>event handler</code> then my <code>setGUI</code> will be like 500 lines long, maybe even more. I tried to create an event handler object in this way:</p>
  
  <pre><code>public class MyListener implements EventHandler{
  @override
  public void handle(Event event) {
          //My code
      }
  }
  </code></pre>
  
  <p>But EventHandler is Abstract and I have to implement several methods.
  Googling I saw this code:</p>
  
  <pre><code>public class AppWithEvents1 extends Application {
      Text text=null;
      public void start(Stage stage) {
          text=new Text(10,50,"Non hai mai cliccato ");
          Button btn = new Button();
          btn.setText("Click me");
          Listener a=new Listener(this);
          btn.addEventHandler(ActionEvent.ACTION, a);
          Group root = new Group(btn);
          root.getChildren().add(text);
          Scene scene = new Scene(root);
          stage.setScene(scene);
          stage.show();
      }
      public void updateText(int n){
          text.setText("Hai cliccato "+n
          +" volte");
      }
      public static void main(String[] args) {
          Application.launch(args);
      }
  } 
  </code></pre>
  
  <p>and he created this class:</p>
  
  <pre><code>class Listener
      implements EventHandler{
          AppWithEvents1 awe=null;
          int counter=0;
          Listener(AppWithEvents1 a){
              awe=a;
          }
      public void handle(Event t) {
          awe.updateText(++counter);
      }
  }
  </code></pre>
  
  <p>Why didn't he implement all abstract methods?</p>