title: How do you close a window from another class in JavaFX?
body: <p>I have been having an issue trying to close a Login window from another Controller class. The Login window is opened in Main.java but the fxml file is controlled by LoginController.java. I have tried to extend the Main.java class to close the stage, but this isn't working. I have tried to make pass through methods as well but did not have much luck. Here is my code:</p>
  
  <p>Main:</p>
  
  <pre><code>public class Main extends Application {
      Stage window;
      Parent root;
  
      @Override
      public void start(Stage primaryStage) {
          window = primaryStage;
          try {
              root = FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
              Scene scene = new Scene(root);
              scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
              window.setScene(scene);
              window.show();
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          launch(args);
  
      }
  }
  </code></pre>
  
  <p>LoginController:</p>
  
  <pre><code>public class LoginController extends Main implements Initializable{
      public LoginModel loginModel = new LoginModel();
  
  
      @FXML
      private Label isConnected;
  
      @FXML
      private TextField txtUsername;
  
      @FXML
      private TextField txtPassword;
  
      @FXML
      private Button loginButton;
  
      @FXML
      private Button cancelButton;
  
      @Override
      public void initialize(URL location, ResourceBundle resources) {
          // TODO Auto-generated method stub
          if (loginModel.isDbConnected()) {
              isConnected.setText("Connected");
          } else {
              isConnected.setText("Not Connected");
          }
      }
  
      public void Login (ActionEvent event) {
          try {
              if (loginModel.isLogin(txtUsername.getText(), txtPassword.getText())) {
                  isConnected.setText("Username and password are correct");
                  ((Node)event.getSource()).getScene().getWindow().hide();
                  Stage primaryStage = new Stage();
                  FXMLLoader loader = new FXMLLoader();
                  Pane root = loader.load(getClass().getResource("/application/Main.fxml").openStream());
                  //UserController userController = (UserController)loader.getController();
                  //userController.GetUser(txtUsername.getText());
                  Scene scene = new Scene(root);
                  scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                  primaryStage.setScene(scene);
                  primaryStage.show();
  
  
              } else {
                  isConnected.setText("Username and password are not correct");
              }
          } catch (SQLException e) {
              isConnected.setText("Username and password are not correct");
              // TODO Auto-generated catch block
              System.out.println(e);
              e.printStackTrace();
          } catch (IOException e) {
              System.out.println(e);
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
      public void cancelLogin() {
          window.close();
      }
  }
  </code></pre>
  
  <p>FXML line of code:</p>
  
  <pre><code>&lt;Button layoutX="110.0" layoutY="125.0" minWidth="70.0" mnemonicParsing="false" onAction="#cancelLogin" text="Cancel" /&gt;
  </code></pre>
  
  <p>I have also tried creating a Main object in the LoginController and using main.window.close(); but that does not seem to work either (this was before extending Main). Any help would be greatly appreaciated, I am not sure what to do.</p>