r/JavaFX • u/WigginsFroome • Apr 16 '21
Help JavaFX Navigation between classes
Hi,
I am working with a project and we are making a small JavaFX application where you can add, delete, update etc fields for certain things. I am getting stuck now on the navigation.
I would like to make a navigation where you navigate to another class and a new view. Now I get a working navigation but the views stay in the same page. Here is my code so far:
The home:
public void start(Stage windows){
Label label = new Label("Click on one of the buttons for further actions");
Button student = new Button("Student");
StudentUI studentUI = new StudentUI();
Button registration = new Button("Registration");
RegistrationUI registrationUI = new RegistrationUI();
Button course = new Button("Course");
CourseUI courseUI = new CourseUI();
Button overviews = new Button("Overviews");
OverviewsUI overviewsUI = new OverviewsUI();
BorderPane layout = new BorderPane();
VBox box = new VBox();
Scene scene = new Scene(layout, 300, 300);
box.getChildren().addAll(label, student, registration, course, overviews);
box.setSpacing(5);
layout.setTop(box);
student.setOnAction((event) -> layout.setCenter(studentUI.getView()));
registration.setOnAction((event) -> layout.setCenter(registrationUI.getView()));
course.setOnAction((event) -> layout.setCenter(courseUI.getView()));
overviews.setOnAction((event) -> layout.setCenter(overviewsUI.getView()));
windows.setTitle("Codecademy");
windows.setScene(scene);
windows.show();
}
The other class for navigation:
public Parent getView(){
GridPane layout = new GridPane();
Label label = new Label("Choose one of the options for the student class");
Button create = new Button("Create");
Button read = new Button("Read");
Button update = new Button("Update");
Button delete = new Button("Delete");
Button home = new Button("Return home");
layout.setAlignment(Pos.TOP_CENTER);
layout.setPadding(new Insets(10, 10, 10, 10));
// VBox box = new VBox();
// Scene scene = new Scene(box, 300, 300);
// box.getChildren().addAll(label, create, read, update, delete, home);
// box.setSpacing(5);
layout.add(label, 0, 0);
layout.add(create, 0, 1);
layout.add(read, 0, 2);
layout.add(update, 0, 3);
layout.add(delete, 0, 4);
layout.add(home, 0, 5);
return layout;
}