title: Handle mouse event anywhere with JavaFX
body: <p>I have a JavaFX application, and I would like to add an event handler for a mouse click anywhere within the scene. The following approach works ok, but not exactly in the way I want to. Here is a sample to illustrate the problem:</p>
  
  <pre class="lang-java prettyprint-override"><code>public void start(Stage primaryStage) {
      root = new AnchorPane();
      scene = new Scene(root,500,200);
      scene.setOnMousePressed(new EventHandler&lt;MouseEvent&gt;() {
          @Override
          public void handle(MouseEvent event) {
              System.out.println("mouse click detected! "+event.getSource());
          }
      });
  
      Button button = new Button("click here");
      root.getChildren().add(button);
  
      primaryStage.setScene(scene);
      primaryStage.show();
  }
  </code></pre>
  
  <p>If I click anywhere in empty space, the <code>EventHandler</code> invokes the <code>handle()</code> method, but if i click the <code>button</code>, the <code>handle()</code> method is not invoked. There are many buttons and other interactive elements in my application, so I need an approach to catch clicks on those elements as well without having to manually add a new handler for every single element.</p>