I am having some troubles with splitting the business logic from a single controller into different controllers. Problem: At the beginning of my application,i ask user for username and password to perform login.After that,and if the login succeeds,i want to show to the user the main scene which includes some basic operation my app can do.The problem is : to perform any basic operation(for example,find groups the user belongs to) i need to know the username the user used when he logged in.At first, i made a single controller and passed to it the username from the login scene.Here is the login method from the login controller:

private void login(){

    boolean loginResult = false;

    try {
//Returns True if the user can login with the given username and password
            loginResult = userRemote.login(username_field.getText(), password_field.getText());

        if (!loginResult) {
            Alert alert = new Alert(Alert.AlertType.ERROR, "The username and/or password are incorrect");
            alert.showAndWait();
        } else {
            loadMain();
        }

    } catch (RemoteException e) {
        e.printStackTrace();
        Alert alert = new Alert(Alert.AlertType.ERROR, "Unable to connect to the server.Please try again later");
        alert.showAndWait();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

If the login succeeds,i load the main scene passing it the username.Here is the code:

FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/fxml/main.fxml"));

        Parent root = loader.load();

        Controller controller = loader.getController();

        controller.init(username_field.getText());

        Stage stage = (Stage) ( (Node) (sign_in_btn) ).getScene().getWindow();

        Scene scene = new Scene(root);

        scene.getStylesheets().add("/gui/view/css/style.css");

        stage.setScene(scene);

In the Controller i save the username in a field,here is the init method that is called :

void init(String username) throws IOException {
    this.username = username;
    //some other code
}

After that i have the username in the my Controller of the main Scene,but the problem is that because of so many operation the controller has to do,the code in that controller is huge(about 800 lines of code).

I want to ask if there is any way to pass the username to other controllers from a single controller? The operations my application performs are : finds tasks of the user,finds groups of the user,creates tasks,creates groups,deletes tasks,delete groups. I use a group of toggle buttons that make the nav bar : enter image description here

So whenever the user clicks any of the toggle buttons,an appropriate operation is performed using the username he gave when he logged in. I want each toggle button to load a single fxml view,and each of the views to have its own controller that does the appropriate action using the username.But i don't know how to save the username and pass it to those controllers. Right know i use a single controller that doesn't use any fxml files,it just creates the appropriate gui,depending on the toggle button that was clicked,and performs the business logic.

0 Answers