Search code examples
javafxjavafx-8

how to open all stage in only one window?


I created a small software in JavaFX. The main window has several buttons that open other pages. But when I click on these buttons, they open new windows like in this picture.

would like all of these pages to open in a single window like a normal software.

That's the code of the events on all this buttons.

@FXML
void Agenda(ActionEvent event) {

}

@FXML
void Informations(ActionEvent event) {
    try{

        FXMLLoader loade = new FXMLLoader(getClass().getResource("/cm/Project/View/Informations.fxml"));
        VBox root = (VBox) loade.load();
        Stage stage = new Stage();
        stage.setTitle("Bernon Storage");
        stage.setScene(new Scene(root));

        stage.show();
    } catch (Exception e){
        e.printStackTrace();
    }

}

@FXML
void Inscription(ActionEvent event) {
    try{
        FXMLLoader loade = new FXMLLoader(getClass().getResource("/cm/Project/View/Inscription.fxml"));
        VBox root = (VBox) loade.load();
        Stage stage = new Stage();
        stage.setTitle("Bernon Storage");
        stage.setScene(new Scene(root));
        stage.show();
    } catch (Exception e){
        e.printStackTrace();
    }

}

@FXML
void Notes(ActionEvent event) {
    try{
        FXMLLoader loade = new FXMLLoader(getClass().getResource("/cm/Project/View/Notes1.fxml"));
        VBox root = (VBox) loade.load();
        Stage stage = new Stage();
        stage.setTitle("Bernon Storage");
        stage.setScene(new Scene(root));
        stage.show();
    } catch (Exception e){
        e.printStackTrace();
    }

}

@FXML
void Staff(ActionEvent event) {
    try{
        FXMLLoader loade = new FXMLLoader(getClass().getResource("/cm/Project/View/Staff.fxml"));
        VBox root = (VBox) loade.load();
        Stage stage = new Stage();
        stage.setTitle("Bernon Storage");
        stage.setScene(new Scene(root));
        stage.show();
    } catch (Exception e){
        e.printStackTrace();
    }

}

@FXML
void Student(ActionEvent event) {
    try{
        FXMLLoader loade = new FXMLLoader(getClass().getResource("/cm/Project/View/Student.fxml"));
        VBox root = (VBox) loade.load();
        Stage stage = new Stage();
        stage.setTitle("Bernon Storage");
        stage.setScene(new Scene(root));
        stage.show();
    } catch (Exception e){
        e.printStackTrace();
    }

}

Please help me to solve this problem. It's the only thing that i have to finish this project.


Solution

  • Please check the below demo for a quick idea of how to load different views in same window.

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ToolBar;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class Window_Demo extends Application {
        BorderPane root;
        @Override
        public void start(Stage primaryStage) throws Exception {
            root = new BorderPane();
            Scene scene = new Scene(root, 500,500);
            primaryStage.setScene(scene);
            primaryStage.show();
    
            Button view1 = new Button("View 1");
            view1.setOnAction(e->{
                StackPane view = new StackPane(); // Load your fxml and get the node
                view.setStyle("-fx-background-color:red;-fx-opacity:.5;");
                root.setCenter(view);
            });
            Button view2 = new Button("View 2");
            view2.setOnAction(e->{
                StackPane view = new StackPane();  // Load your fxml and get the node
                view.setStyle("-fx-background-color:green;-fx-opacity:.5;");
                root.setCenter(view);
            });
            Button view3 = new Button("View 3");
            view3.setOnAction(e->{
                StackPane view = new StackPane();  // Load your fxml and get the node
                view.setStyle("-fx-background-color:blue;-fx-opacity:.5;");
                root.setCenter(view);
            });
            ToolBar toolBar = new ToolBar(view1,view2,view3);
            root.setTop(toolBar);
        }
    }