title: JavaFx how to avoid creating one huge controller
body: <p>I have an app in JavaFX, which has main scene with menu and toolbar, and smaller scenes, which are injected into this main scene, after one of menu buttons are being pressed. </p>
  
  <p>Now, HomeCntroller is responsible for either scene components: Home Scene (with toolbar and menu), and injected scene. This leads me to create massive, huge and very unprofessional controller if number of injected scenes is more than one. </p>
  
  <p>How to split controller responsibility? </p>
  
  <p>Now my Controller looks like this:
  changeDashboardPane method injects smaller Pane into my main HomePane.</p>
  
  <pre><code>@Component
  @RequiredArgsConstructor(onConstructor = @__(@Autowired) )
  public class HomeController extends AbstractController {
      private static final Logger LOG = Logger.getLogger(HomeController.class);
  
      private final BudgetProfileService budgetProfileService;
  
      @FXML
      private Label usernameLabel;
  
      @FXML
      private ComboBox&lt;String&gt; budgetProfilesComboBox; 
  
      @FXML
      private AnchorPane dashBoardPane;
  
      @FXML
      public void initialize() {
          refreshUsernameLabel();
          getAllBudgetProfiles();
          changeDashboardPane(PaneFactoryKeys.FINANCES_PANE);
      }
  
      private void refreshUsernameLabel() {
          String username = UserAccountProvider.getLoggedUser().getUsername();
          usernameLabel.setText(username);
      }
  
      private void getAllBudgetProfiles() {
          List&lt;String&gt; budgetProfileNames = budgetProfileService.getAllBudgetProfileNames();
          if (!budgetProfileNames.isEmpty()) {
              budgetProfilesComboBox.getItems().clear();
              budgetProfilesComboBox.getItems().addAll(budgetProfileNames);
          }
      }
  
      @FXML
      public void handleFinancesButtonAction() {
          changeDashboardPane(PaneFactoryKeys.FINANCES_PANE);
      }
  
      @FXML
      public void handlePeriodButtonAction() {
          changeDashboardPane(PaneFactoryKeys.PERIOD_PANE);
      }
  
      @FXML
      public void handleStatisticsButtonAction() {
          changeDashboardPane(PaneFactoryKeys.STATISTICS_PANE);
      }
  
      @FXML
      public void handleSettingsButtonAction() {
          changeDashboardPane(PaneFactoryKeys.SETTINGS_PANE);
      }
  
      private final void changeDashboardPane(String paneFactoryKey) {
          double injectedPanePosition = 0.0;
          Pane paneToChange = getPaneFromFactory(paneFactoryKey);
          dashBoardPane.getChildren().clear();
          AnchorPane.setTopAnchor(paneToChange, injectedPanePosition);
          dashBoardPane.getChildren().add(paneToChange);
      }
  
  }
  </code></pre>
  
  <p>To get this more clear, screens:</p>
  
  <p><a href="https://postimg.org/image/4byk98hfx/" rel="nofollow">without injected second pane</a>
  <a href="https://postimg.org/image/3nppqaiq5/" rel="nofollow">with injected second pane</a></p>
  
  <p>Any ideas guys? </p>