0

I'm creating a JavaFX application. After the login screen I want the user to see a slightly different layout if he's also administrator.

How do I change the view of the MainWindow right after the LoginWindow according to this?

(I tried to lighten the code as much as I could)

Here's the code of the LoginController.java class, (among the other things) responsible of localizing admin users:

public class LoginController {
    // All @FXMLs needed ...
    @FXML
    private Button loginButton;

    public void initialize() {
        loginButton.setOnAction(event -> {
            if (authorize()) {
                MainLoader mainLoader = new MainLoader();
                mainLoader.loadMain();
                // Close login screen
            }
        });

    private boolean authorize() {
        // returns true if the user is authorized
    }

    private boolean isAdmin() {
        // returns true if the user is also admin
    }
}

As you can see, LoginController calls MainLoader.java:

public class MainLoader {
    public void loadMain() {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("../../view/main_screen.fxml"));
            Stage stage = new Stage();
            stage.setScene(new Scene(root));
            stage.show();
        } catch (IOException ex) {
            // Manage exception
        }
    }
}

Here's the code of the MainController.java class, responsible of setting up correctly the Main window of my application:

public class MainController {
    // All @FXMLs needed ...
    @FXML
    private Tab adminTab;

    public void initialize() {
        // Setting all controls of previous widgets...

        // IF IS ADMIN THEN:
            adminTab.setDisable(false);
    }
}
Robb1
  • 4,587
  • 6
  • 31
  • 60
  • Thanks for your answer @SedrickJefferson! But how? Should I add a parameter to the `loadMain(boolean isAdmin)`? And after this, how to impact on the controller of the `MainWindow`? – Robb1 Jun 13 '17 at 22:08
  • You can't pass a parameter to an FXML file: you have to [pass the parameter to the `MainController`](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml). You can disable or enable the admin functionality there. – James_D Jun 13 '17 at 22:17

0 Answers0