title: how do I get an element in JavaFx using an id?
body: <p>I am new to FXML and I am trying to create a handler for all of the button clicks using a <code>switch</code>. However, in order to do so, I need to get the elements using and id. I have tried the following but for some reason (maybe because I am doing it in the controller class and not on the main) I get a stack overflow exception.</p>
  
  <pre><code>public class ViewController {
      public Button exitBtn;
  
      public ViewController() throws IOException {
           Parent root = FXMLLoader.load(getClass().getResource("mainWindow.fxml"));
           Scene scene = new Scene(root);
  
           exitBtn = (Button) scene.lookup("#exitBtn");
      }
  }
  </code></pre>
  
  <p>So how will I get an element (for example a button) using it's id as a reference?</p>
  
  <p>The fxml block for the button is:</p>
  
  <pre><code>&lt;Button fx:id="exitBtn" contentDisplay="CENTER" mnemonicParsing="false"
          onAction="#handleButtonClick" text="Exit" HBox.hgrow="NEVER" HBox.margin="$x1"/&gt;
  </code></pre>