0

i am quite new to javafx. I saw many ways of switching between screens but they somehow didn't work after some thinking, i thought of using this logic. Please i need to know if it is advice-able for me to continue with it, before i go too far in the project.

 @FXML
public void nextAfterPassangerButtonClicked() throws Exception {

    MainScreenDatabaseHandler a = new MainScreenDatabaseHandler(getId(), getFirstName(), getLastName(), getOtherName(), getSexSelection(), getMobileNumber(), getEmergencyContact(), getHomeAdress());
    //send collected data to database
    passangerPaymentAnchorPane.getChildren().remove(0);
    Node node = FXMLLoader.load(getClass().getResource("CargoPayment.fxml"));
    passangerPaymentAnchorPane.getChildren().add(node);
}

//Handle All Menu Bar Buttons
@FXML
public void startPageBarButtonClicked() throws Exception {
    passangerPaymentAnchorPane.getChildren().remove(0);
    mainAnchor.getChildren().remove(0);
    Node node = FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
    mainAnchor.getChildren().add(node);
}

public void allPassangersMenuBarButtonClicked() throws Exception {

    passangerPaymentAnchorPane.getChildren().remove(0);
    Node node = FXMLLoader.load(getClass().getResource("AllPassangersView.fxml"));
    passangerPaymentAnchorPane.getChildren().add(node);
}

when i click on one of the buttons, it removes the current scene and loads the related fxml. Thank You. The passangerPaymentAnchorPane acts like the mother pane on which other fxml loads and unload

Joshua. O
  • 71
  • 1
  • 9

2 Answers2

1

There is nothing wrong with the main approach, namely there is some layout that acts like a container for loading fxml files. Its children will be replaced on some event action. I suggest to refactor your code before project gets grown, like a loadView(String fxmlFile) method where the repetitive "layout.remove - fxmlloader.load() - layout.add()" pairs above are refactored into. Additionally, since the parsing and loading of fxml file is an expensive work, do it lazily and update the view only, like:

Pane cargoPaymentPane;
CargoPaymentController cargoPaymentController;
FXMLLoader fxmlLoader = new FXMLLoader();

public void loadCargoPaymentView(String newCargoData) {
    if(cargoPaymentPane == null) { 
        cargoPaymentPane = fxmlLoader.load(getClass().getResource("CargoPayment.fxml").openStream());
        cargoPaymentController = (CargoPaymentController) fxmlLoader.getController();
    }
    cargoPaymentController.updateView(newCargoData);
}
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
1

I have also struggled with javaFx multiscreen issue. Finally I solved as follows. I am added the code in github

https://github.com/nrkkalyan/javafx

Feel free to comment or any suggestions.

nrkkalyan
  • 179
  • 2
  • 5