1

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.

JrMbh7
  • 58
  • 7

1 Answers1

3

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);
    }
}
Sai Dandem
  • 8,229
  • 11
  • 26
  • 1
    I understood your example. But I have several classes independent of the main one. I don't know how to get them displayed from the main one. – JrMbh7 Jan 09 '20 at 22:41
  • When you click on a button, you can get the scene from the button and set the root there, e.g. using some variables from Sai's example, in the on action handler for view3, write `view3.getScene().getRoot().setCenter(view);` – jewelsea Jan 10 '20 at 06:19
  • 1
    I solve it by typing `Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();` – JrMbh7 Jan 10 '20 at 12:13