Open a new Window in JavaFX
View more Tutorials:
In JavaFX, to create a window, you use Stage class.

There are three modelities that you can apply to the Stage through the stage.initModality(Modelity) method.
- Modelity.NONE
- Modelity.WINDOW_MODAL
- Modelity.APPLICATION_MODAL
When creating a new Stage, you can set up a parent window for it (also called the window owning it), via the stage.initOwner(parentStage) method.
No | Modelity | Description |
1 | Modelity.NONE | When you open a new window with this modelity, the new window will be independent from the parent window. You can interact with the parent window, or close it without affecting the new window. |
2 | Modelity.WINDOW_MODAL | When you open a new window with this modelity, it will lock the parent window. You can not interact with the parent window until this window is closed. |
3 | Modelity.APPLICATION_MODAL | When you open a new window with this modelity, it will lock any other windows of the application. You can not interact with any other windows until this window is closed. |
For example, when you press a Button on the window 1, it will open the second window with the default modelity (Modeless Window / Modelity.NONE).

OpenNewModelessWindowExample.java
package org.o7planning.javafx.window; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class OpenNewModelessWindowExample extends Application { @Override public void start(final Stage primaryStage) { Button button = new Button(); button.setText("Open a New Window"); button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { Label secondLabel = new Label("I'm a Label on new Window"); StackPane secondaryLayout = new StackPane(); secondaryLayout.getChildren().add(secondLabel); Scene secondScene = new Scene(secondaryLayout, 230, 100); // New window (Stage) Stage newWindow = new Stage(); newWindow.setTitle("Second Stage"); newWindow.setScene(secondScene); // Set position of second window, related to primary window. newWindow.setX(primaryStage.getX() + 200); newWindow.setY(primaryStage.getY() + 100); newWindow.show(); } }); StackPane root = new StackPane(); root.getChildren().add(button); Scene scene = new Scene(root, 450, 250); primaryStage.setTitle("JavaFX Open a new Window (o7planning.org)"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
For example, when you press a Button on the window 1, it will open a new window with the Modal Window modelity (Modelity.WINDOW_MODAL) . When the new window still displays you can not interact with the father window.

OpenNewModelWindowExample.java
package org.o7planning.javafx.window; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.StackPane; import javafx.stage.Modality; import javafx.stage.Stage; public class OpenNewModelWindowExample extends Application { @Override public void start(final Stage primaryStage) { Button button = new Button(); button.setText("Open a New Window"); button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { Label secondLabel = new Label("I'm a Label on new Window"); StackPane secondaryLayout = new StackPane(); secondaryLayout.getChildren().add(secondLabel); Scene secondScene = new Scene(secondaryLayout, 230, 100); // New window (Stage) Stage newWindow = new Stage(); newWindow.setTitle("Second Stage"); newWindow.setScene(secondScene); // Specifies the modality for new window. newWindow.initModality(Modality.WINDOW_MODAL); // Specifies the owner Window (parent) for new window newWindow.initOwner(primaryStage); // Set position of second window, related to primary window. newWindow.setX(primaryStage.getX() + 200); newWindow.setY(primaryStage.getY() + 100); newWindow.show(); } }); StackPane root = new StackPane(); root.getChildren().add(button); Scene scene = new Scene(root, 450, 250); primaryStage.setTitle("JavaFX Open a new Window (o7planning.org)"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }