title: Event handing issue in Controller in JavaFX and MVC (Not FXML)
body: <p>I am trying to handle event inside controller. How can i make "Create Profile" button work inside controller. Here are my classes:</p>
  
  <p>main class</p>
  
  <pre><code>public class ApplicationLoader extends Application {
  
  private OptionsModuleChooserRootPane view;
  
  @Override
  public void init() {
      StudentProfile model = new StudentProfile();
      view = new OptionsModuleChooserRootPane();
      new OptionsModuleChooserController(view, model);    
  }
  
  @Override
  public void start(Stage stage) throws Exception {
      stage.setMinWidth(530); 
      stage.setMinHeight(550);
      stage.setTitle("Final Year Module Chooser Tool");
      stage.setScene(new Scene(view));
      stage.show();
  }
  
  public static void main(String[] args) {
      launch(args);
  }
  
  }
  </code></pre>
  
  <p>Main View</p>
  
  <pre><code>public class OptionsModuleChooserRootPane extends BorderPane {
  
  Menu fileMenu;
  Menu helpMenu;
  MenuBar menuBar;
  TabPane tabPane;
  List&lt;Tab&gt; tabs;
  
  public CreateProfileTab profilePane;
  public SelectModulesTab modulesPane;
  public OverviewSelectionPane overviewPane;
  
  public OptionsModuleChooserRootPane() {
  
      fileMenu = new Menu("File");
      helpMenu = new Menu("Help");
      menuBar = new MenuBar();
      tabPane = new TabPane();
      tabs = new ArrayList&lt;&gt;();
  
      //------------------ File menu ----------------------------
      fileMenu.getItems().add(new MenuItem("Load Student Data"));
      fileMenu.getItems().add(new MenuItem("Save Student Data"));
      fileMenu.getItems().add(new MenuItem("Exit"));
  
      //----------------- Help menu -----------------------------
      helpMenu.getItems().add(new MenuItem("About"));
  
      //----------------- MenuBar containing all menus ---------
      menuBar.getMenus().addAll(fileMenu,helpMenu);
      this.setTop(menuBar);       
  
      profilePane = new CreateProfileTab();
      modulesPane = new SelectModulesTab();
      overviewPane = new OverviewSelectionPane();
  
      //-------------------------------- Tabs -----------------------------------------------------
      tabs.add(addNewTab(tabPane, "Create Profile", profilePane, false)); 
      tabs.add(addNewTab(tabPane, "Select Modules", modulesPane, false));
      tabs.add(addNewTab(tabPane, "Overview Selection", overviewPane, false));
  
      this.setCenter(tabPane);
  }
  
  private Tab addNewTab(final TabPane tabPane, String newTabName, Pane newTabContent, boolean isCloseable) {
      Tab newTab = new Tab(newTabName);
      newTab.setContent(newTabContent);
      newTab.setClosable(isCloseable);
  
      tabPane.getTabs().add(newTab);
      return newTab;
  
  }
  }
  </code></pre>
  
  <p>I have created separate files for all three tabs. Here is the "Create Profile" tab class which has the button i am trying to handle event of.</p>
  
  <pre><code>public class CreateProfileTab extends GridPane {
  
  private Label lSelectCousrse;
  private ComboBox&lt;Course&gt; cboCourses;
  private Label lPNumber;
  private TextField tfPNumber;
  private Label lFirstName;
  private TextField tfFirstName;
  private Label lSurname;
  private TextField tfSurname;
  private Label lEmail;
  private TextField tfEmail;
  private Label lDate;
  private DatePicker datePicker;
  private HBox hboxDate;
  private Button btnCreateProfile;
  
  private StudentProfile student = null;
  private Name name;
  
  public CreateProfileTab(){
  
      this.setMinSize(400, 200); 
      this.setPadding(new Insets(10, 10, 10, 10)); 
      this.setVgap(15); 
      this.setHgap(20);       
      this.setAlignment(Pos.CENTER); 
  
      lSelectCousrse = new Label("Select course:");
      cboCourses = new ComboBox&lt;Course&gt;();
      lPNumber = new Label("Input P number:");
      tfPNumber = new TextField();
      lFirstName = new Label("Input first name:");
      tfFirstName = new TextField();
      lSurname = new Label("Input surname");
      tfSurname = new TextField();
      lEmail = new Label("Input email:");
      tfEmail = new TextField();
      lDate = new Label("Input date:");
      datePicker = new DatePicker();
      hboxDate = new HBox(datePicker);
      btnCreateProfile = new Button("Create Profile");
  
      this.add(lSelectCousrse, 0, 0);
      GridPane.setHalignment(lSelectCousrse, HPos.RIGHT);
      this.add(cboCourses, 1, 0);
      this.add(lPNumber, 0, 1);
      GridPane.setHalignment(lPNumber, HPos.RIGHT);
      this.add(tfPNumber, 1, 1);
      this.add(lFirstName,0,2);
      GridPane.setHalignment(lFirstName, HPos.RIGHT);
      this.add(tfFirstName,1,2);
      this.add(lSurname,0,3);
      GridPane.setHalignment(lSurname, HPos.RIGHT);
      this.add(tfSurname,1,3);
      this.add(lEmail,0,4);
      GridPane.setHalignment(lEmail, HPos.RIGHT);
      this.add(tfEmail,1,4);
      this.add(lDate,0,5);
      GridPane.setHalignment(lDate, HPos.RIGHT);
      this.add(hboxDate,1,5);
      this.add(btnCreateProfile,1,6);
  
  
  }
  
  public void populateComboBoxWithCourses(Course[] courses) {
      cboCourses.getItems().addAll(courses);
      cboCourses.getSelectionModel().select(0);
  }
  
  
  public Button getButton(){
      return this.btnCreateProfile;
  }
  
  }
  </code></pre>
  
  <p>And here is my controller</p>
  
  <pre><code>public class OptionsModuleChooserController implements EventHandler&lt;ActionEvent&gt;  {
  
  private OptionsModuleChooserRootPane view;
  private StudentProfile model;
  
  public OptionsModuleChooserController(OptionsModuleChooserRootPane view, StudentProfile model) {
      this.model = model;
      this.view = view;       
      this.view.profilePane.populateComboBoxWithCourses(setupAndRetrieveCourses());
  
  }
  
  private Course[] setupAndRetrieveCourses() {
      .......
  }
  
  
  @Override
  public void handle(ActionEvent event) {
      final Object source = event.getSource();
  
      if (source.equals(this.view.profilePane.getButton())) {
          System.out.println("Button has been pressed!");
      }
  
  }
  }
  </code></pre>