I am creating a program in which I have a MainMenu
class which represents the main menu of the program. The main menu will have several different options to choose from through buttons, one of which is called buttonToSubMenu1
. This option will lead to a new menu (a sub-menu) represented by a class called SubMenu1
. SubMenu1
will have have multiple buttons as well (such as buttonToSubSubMenu1
) and each will lead to another menu (a sub-sub-menu). I am trying to use the MVC pattern, and what I have done so far is to create one view-class for each menu, and one controller corresponding to that view and these will modify the model as necessary. The problem is that, even if in my code below I have used only 1 or 2 choices in each menu, I will have a lot more in the real version and I will end up with a lot of different classes representing different views and controllers. Therefore I am wondering if there is any way to write or design this program with multiple menus and sub-menus in a more efficient way in terms of encapsulation, readability etc. and to avoid making a class for each separate view and controller, while still following the MVC pattern. Is it better to have everything in one view and one controller? The scene I use when I first start the program is MainMenu
. My code is below:
MainMenu
class:
public class MainMenu extends VBox {
public class MainMenu extends VBox {
private Button buttonToSubMenu1;
private MainController controller
public MainMenu() {
this.controller = new MainController(this);
this.buttonToSubMenu1 = new Button("Press here to go to SubMenu1");
createUiComponents();
addEventHandlers();
}
private void createUiComponents()
{/* creates the UI components */}
}
private void addEventHandlers(MainController controller) {
EventHandler<ActionEvent> buttonHandler = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
if(actionEvent.getSource() == buttonToSubMenu1) {
controller.handleSubMenu1Event();
}
}
};
buttonToSubMenu1.setOnAction(buttonHandler);
}
public void setScreenToSubMenu1 (SubMenu1 subMenu1) {
buttonToSubMenu1.getScene().setRoot(subMenu1);
}
}
MainController
class:
public class MainController {
private MainMenu mainMenu;
public MainController(MainMenu mainMenu) {
this.mainMenu = mainMenu;
}
public void handleSubMenu1Event() {
/* change and load from model as necessary */
SubMenu1 subMenu1 = new SubMenu1();
mainMenu.setScreenToSubMenu1(subMenu1);
}
}
SubMenu1
class:
public class SubMenu1 extends VBox {
private Button buttonToSubSubMenu1, buttonToMainMenu;
private SubController1 controller;
public SubMenu1() {
this.controller = new SubController1(this);
this.buttonToSubSubMenu1 = new Button("Press here to go to SubSubMenu1");
this.buttonToMainMenu = new Button("Press here to go back to main menu");
createUiComponents();
addEventHandlers(controller);
}
private void createUiComponents()
{/* creates the UI components */}
}
private void addEventHandlers(SubController1 controller) {
EventHandler<ActionEvent> buttonHandler = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
if(actionEvent.getSource() == buttonToMainMenu {
controller.handleMainMenuEvent();
} else if(actionEvent.getSource() == buttonToSubSubMenu1) {
controller.handleSubSubMenu1Event();
}
}
};
buttonToMainMenu.setOnAction(buttonHandler);
buttonToSubSubMenu1.setOnAction(buttonHandler);
}
public void setScreenToMainMenu(MainMenu mainMenu) {
buttonToMainMenu.getScene().setRoot(mainMenu);
}
public void setScreenToSubSubMenu1(SubSubMenu1 subSubMenu1) {
buttonToSubSubMenu1.getScene().setRoot(subSubMenu1);
}
}
SubController1
class:
public class SubController1 {
private SubMenu1 subMenu1;
public SubController1(SubMenu1 subMenu1) {
this.subMenu1 = subMenu1;
}
public void handleMainMenuEvent() {
MainMenu mainMenu = new MainMenu();
subMenu1.setScreenToMainMenu(mainMenu);
}
public void handleSubSubMenu1Event() {
SubSubMenu1 subSubMenu1 = new SubSubMenu1();
subMenu1.setScreenToSubSubMenu1(subSubMenu1);
}
}
Also tell me if there are any obvious mistakes I have done in my code above.
7