1

I've searched the web for an answer and can't really make sense of them into my code. I'm still new at javafx, and coding in general.

My controller class "MainController.java" functions all my buttons, labels, text fields etc. I have created a login button which will load the Main scene on a successful login. However, as the main scene loads as a separate window, the old initial login scene still remains and I was wondering if there's an easy way to control my scenes from a controller class.

Here is my controller class bit of the code:

// Login button method call
    public void Login() throws Exception {
        Thread.sleep(500); // login delay

        if (userInput.getText().equals("user") && passInput.getText().equals("pass")){
            // load main scene
            Parent root1 = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
            Scene scene1 = new Scene(root1);
            Stage mainScene = new Stage();
            scene1.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            mainScene.setScene(scene1);
            mainScene.initStyle(StageStyle.UNDECORATED);
            mainScene.show();
        }else {
            status = "Invalid credentials";
            statusLabel.setText(status);

        }
    }

I'm unsure of how to hide my old initial scene (the login scene). I've tried to do

loginScene.hide();

but I can't seem to link the two. I'm quite new at this so help would be appreciated. Thanks :D

Bruce219
  • 37
  • 2
  • 11

1 Answers1

0

Create in the main class

 public static Scene mainLoginScene;

And in the Main method add

mainLoginScene = loginScene;

In the controller class you can now

MainClass.mainLoginScene.hide();

I hope I could help you, my English is not perfect and I'm new here :)

Simon
  • 2,686
  • 2
  • 31
  • 43