0

I have designed two MVC structures(not sure if this is the right term)

For a home.fxml and login.fxml.

How do I on successful login change to the home.fxml view with its controller, as well as saving the current user for the home MVC structure to use.

What I have tried:

I was able to create an EvenHandler in the main.java class and pass that to the LoginView.java class. And the event is hit in LoginView.java the even in main.java would be fired and the scene would switch with this code:

EventHandler<MouseEvent> changeToHome = new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent t) {
        try{
            Parent root = FXMLLoader.load(getClass().getResource("Home.fxml"));
            Scene scene = new Scene(root);

            primaryStage.setScene(scene);
        }catch (IOException err){

        }
    }
};

But if I do it like this, I can't see a way to pass the authenticated user to the UserController(User MVC).

How would I go about doing this?

Shekhar Rai
  • 2,008
  • 2
  • 22
  • 25
HeeysamH
  • 187
  • 9
  • Does this answer your question? [Applying MVC With JavaFx](https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx) – SedJ601 Dec 04 '19 at 20:26
  • _I can't see a way to pass the authenticated user to the userController_ neither can I without [mre] – c0der Dec 05 '19 at 12:40

1 Answers1

0

I have an application in which I have a login form and then from that a Main Screen is shown. In the MainScreen resides most of my code.

The workaround I found (this application makes not use of any framework to fully implement MVC) was to pass the argument to the MainScreen Controller, then I propagate it over the other views. I am not sure if that is what you are looking for whatsoever, but, it might help you picture other approaches to this problem:

Stage stage = (Stage) root1.getScene().getWindow();
stage.close();
Stage stage2 = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/SIBAC/View/Application/MainScreen.fxml"));
Parent newLoadedPane = loader.load();
MainScreenController MSController = loader.getController();
MSController.setUsrName(UsrName);
MSController.permission();
MSController.setLabelsLogOut();
MSController.startHome();

This is inside the LoginController.java file, which, is the one in charge of handling all of the authentication logic. Of course, this will happen upon successful login.

Of course, there are plenty of solutions out there to fully implement MVC or other patterns in a better way (MVVMFX, Griffon, JRebirth, etc)

MattV
  • 1
  • 4