@FXML private Button closeBtn; Stage currentStage = (Stage)closeBtn.getScene().getWindow(); currentStage.close();
Another way is to define a static getter for Stage and Access it
Main class
public class Main extends Application { private static Stage primaryStage;
Now you can access this stage by calling
Main.getPrimaryStage ()
In the controller class
public class Controller { public void onMouseClickAction(ActionEvent e) { Stage s = Main.getPrimaryStage(); s.close(); } }
source share