title: How to open new stage and close previous stage between classes
body: <p>I try to open new stage and close previous stage between classes but i couldn't success in javafx. maybe if we use singleton pattern , it can run successfully. when you press button , first window will close and second window open.</p>
  
  <p>Here is code;</p>
  
  <p>First class:</p>
  
  <pre><code> public class First extends Application {
  
     public  Stage primaryStage2,primaryStage3;
  
    Second v=new Second();
  
       @Override
     public void start(Stage primaryStage) {
  
  
  Button btn = new Button();
  btn.setText("Press");
  
  this.primaryStage3=primaryStage;
  
  btn.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  
      @Override
      public void handle(ActionEvent event) {
  
        v.open();
      }
     });
  
  StackPane root = new StackPane();
  root.getChildren().add(btn);      
  Scene scene = new Scene(root, 300, 250);
  primaryStage.setScene(scene);
  primaryStage.show();
   }
  
  
    public static void main(String[] args) {
  launch(args);
    }
  
  
           }
  </code></pre>
  
  <p>Second class :</p>
  
  <pre><code>   public class Second {
  
   Stage  primaryStage2;
  
     First a=new First();
  
   public void open(){
  
    primaryStage2=new Stage();
   StackPane root = new StackPane();              
   Scene scene = new Scene(root, 300, 250);       
  primaryStage2.setScene(scene);
   a.primaryStage3.close();
  primaryStage2.show();
   }
  }
  </code></pre>