How to change the scene itself in the class controller. After all, the Stage is created in the main class, and I can not reach it. main class:

 @Override public void start (Stage primaryStage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("/login.fxml")); Scene scene = new Scene(root); primaryStage = new Stage(); primaryStage.setScene(scene); primaryStage.show(); } 

Controller class:

 @FXML private void pressRegistration() { try { Parent root = FXMLLoader.load(getClass().getResource("/registration.fxml")); Scene scene = new Scene(root); Stage primaryStage = new Stage(); primaryStage.setScene(scene); primaryStage.show(); } catch (IOException e) { e.printStackTrace(); } } 

By clicking on the button, another window is created ( Stage ). How do you get to the current Stage (which is in main ), and call the setScene() method?

    1 answer 1

    You can send a Stage instance to the controller. Just need to slightly change the start method:

      @Override public void start (Stage primaryStage) throws Exception { //Создаем загрузчика FXMLLoader loader = new FXMLLoader(getClass().getResource("/login.fxml"")); //Загружаем (место AnchorPane должна быть главная панель твоего fxml): AnchorPane root = (AnchorPane) loader.load(); //Теперь мы можем получить экземпляр контроллера для root (вместо MyController - Имя класса контроллера): MyController controller = loader.getController(); Scene scene = new Scene(root); primaryStage = new Stage(); //теперь передаем экземпляр Stage в контроллер: controller.setPrimaryStage(primaryStage); primaryStage.setScene(scene); primaryStage.show(); } 

    Accordingly, the Stage primaryStage field and the setter for it must be added to the controller class.