How to Create Multiple Scenes and Switch Between Scenes in JavaFX


In this article, we show how to create multiple scenes and switch between scenes in JavaFX.

In JavaFX, an application can only have one stage but that stage can have 1 or several scenes.

Therefore, we can create multiple scenes for a given JavaFX application.

In this program, we will create 2 scenes and show how to switch between the scenes in our code.

So the Java code is shown below.

So just like any JavaFX application, you need to import several packages. You're more than likely familiar with this.

We then create a class that extends the Application. Therefore, we inherit all the functionality of the Application class, which has a lot of classes and methods that we need to use.

You want to declare all your scenes before the start() method. Since we create 2 scenes in this application, we create 2 scenes, scene1 and scene2.

We then write our start() function.

We set the title to "My First JavaFX GUI".

We then create scene1.

To scene 1, we have a label, label1, with the text, "This is the first scene".

We have a button, button1, with the text, "Go to scene 2".

We then add event handling to button1, so that when the button is clicked, we can go to another scene. When button1 is clicked, we set the scene to scene 2. This way, we are able to switch from one scene to another. This expression is a lambda expression which allows for easy event handling in JavaFX.

We then create the layout for the window. VBox is a layout in which the elements are stacked vertically on top of each other. We pass in the parameter, 20, so that the elements are spaced 20px apart (so that they're stacked directly on top of each other).

We then take the layout and add hte label1 and button1 elements. Again, these are stacked vertically, not on the same line. If you wanted them on the same line, you could use HBox (standing for horizontal box).

We then initialize the scene. We set the layout and the width and height.

We then go on to create scene2.

Basically, to make this short, we are all the same elements, a label and a button as before. However, to the button, we attach event handling so that when the button is clicked, it goes to scene 1. This way, we are able to switch to scene 1 on the scene 2 page and switch to scene 2 on the scene 1 page.

You can add as many scenes as you want to.

Being that we have created 2 scenes, you have to specify which scene is shown first.

We, therefore, set the primaryStage to scene1 through the setScene() function.

We then show the window through the line,;

This way, we are able to show the window.

And this is all that is required to create multiple scenes and switch between the scenes. All that is necessary is to add event handling to an element such as a button in a scene and use the setScene() function to specify which scene you would like to go to.

Related Resources

How to Add a Title to a Window Using JavaFX

HTML Comment Box is loading comments...