Search code examples
javajavafx-2javafx-8stage

How to open new stage and close previous stage between classes


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.

Here is code;

First class:

 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<ActionEvent>() {

    @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);
  }


         }

Second class :

   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();
 }
}

Solution

  • Why do you want to create new Stage? You can easily create different scenes as you need. And change the scene as you require.

    Try the code below

    Main class

    package application;
    import javafx.application.Application;
    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.BorderPane;
    public class Main extends Application 
    {    
    BorderPane root = new BorderPane();
    Button btn=new Button("Click Here");
    Stage primaryStage;
    Scene scene ;
    public Main()
    {
        btn.setPrefWidth(75);
        btn.setPrefHeight(15);
    
        btn.setOnAction((e)->
        {
            SubClass s=new SubClass(primaryStage);
        });
    }
    
    @Override
    public void start(Stage primaryStage)
    {
        try {
            this.primaryStage=primaryStage;
            //          root.getChildren().add(btn);
    
            scene = initStage();
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    
    private Scene initStage()
    {
        root.setCenter(btn);
        return new Scene(root,150,150);
    }
    
    
    public static void main(String[] args) {
        launch(args);
    }
    }
    

    Sub Class

    package application;
    
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class SubClass
    {
    
    StackPane root = new StackPane();
    Stage stage;
    
    public SubClass(Stage stage)
    {
        Scene scene=new Scene(root,300,300);
        stage.setScene(scene);
        stage.show();
    }
    }
    

    I would suggest you to try changing the scene rather than the stage. All the best