Проблема с обработкой событий в контроллере в JavaFX и MVC (не FXML) - PullRequest
0 голосов
/ 11 апреля 2019

Я пытаюсь обработать событие внутри контроллера. Как сделать так, чтобы кнопка «Создать профиль» работала внутри контроллера. Вот мои занятия:

основной класс

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);
}

}

Главный вид

public class OptionsModuleChooserRootPane extends BorderPane {

Menu fileMenu;
Menu helpMenu;
MenuBar menuBar;
TabPane tabPane;
List<Tab> 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<>();

    //------------------ 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;

}
}

Я создал отдельные файлы для всех трех вкладок. Вот класс вкладки «Создать профиль», в котором есть кнопка, которую я пытаюсь обработать.

public class CreateProfileTab extends GridPane {

private Label lSelectCousrse;
private ComboBox<Course> 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<Course>();
    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;
}

}

А вот и мой контроллер

public class OptionsModuleChooserController implements EventHandler<ActionEvent>  {

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!");
    }

}
}
...