title: Can I automatically generate controller classes from FXML?
body: <p>As I understand it, when using <em>FXML</em> to describe a <em>Java FX</em> scene, the controller class is written manually and it's member variables and methods can then be referenced from the <code>.fxml</code> file. When loading the scene using the <code>FXMLLoader</code>, member variables are set to the corresponding scene elements and methods are wired up to the corresponding events automatically. This works but is very cumbersome as changes need to be done in two places and any mistakes will only show up at runtime.</p>
  
  <p>I've seen other GUI frameworks that allow you to instead generate the controller from a scene description as an abstract class which needs to be implemented to access the scene elements and handle the events. An example of what I mean:</p>
  
  <p>I would create the following <code>.fxml</code> file (e.g. using the <em>JavaFX Scene Builder</em>):</p>
  
  <pre><code>&lt;AnchorPane ... &gt;
    &lt;children&gt;
       &lt;Button fx:id="button" ... text="Button" onAction="#buttonPressed" /&gt;
    &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>Somewhere in my build process, the following <code>.java</code> file would be created (e.g. using a <em>Maven</em> plugin):</p>
  
  <pre><code>abstract class TestController {
      protected final Parent root;
      protected final Button button;
  
      {
          // Load test.fxml file
          // Assign scene elements to root and button
          // Attach event handler to the button that calls buttonClicked()
      }
  
      protected abstract void buttonClicked(ActionEvent event);
  }
  </code></pre>
  
  <p>I could then, possibly multiple times, create a concrete implementation of that controller:</p>
  
  <pre><code>final class TestControllerImpl extends TestController {
      TestControllerImpl(String buttonLabel) {
          button.setText(buttonLabel);
      }
  
      @Override
      protected void buttonClicked(ActionEvent event) {
          button.setText("I've been clicked! What a great day!");
      }
  }
  </code></pre>
  
  <p>Is there a project with the goal to do this? Or is there a problem with this approach applied to <em>FXML</em>?</p>
  
  <p>I see the following benefits from this approach:</p>
  
  <ul>
  <li>Declarations for member variables and methods for the controller are automatically generated.</li>
  <li>All member variables are <em>final</em> and <em>protected</em> instead of <em>non-final</em> and either <em>public</em> or annotated.</li>
  <li>The same for methods, they are <em>protected</em> instead of either <em>public</em> or annotated.</li>
  <li>Not implementing a method or misspelling it's name will lead to a compiler error.</li>
  <li>Programmatic setup of the scene can be done in the constructor instead of an <code>initialize()</code> method because the constructor will run <em>after</em> the scene has been loaded and its elements assigned to the member variables.</li>
  </ul>