How to Open a New Window in JavaFX

If you are just getting started on JavaFX, Please read this JavaFX tutorial first. Usually a non trivial JavaFX application will have more than one window(stage). The following Java example shows how to open a new window in JavaFX. It also shows how to pass data from one stage(window) to another stage.

Please note that the JavaFX program below requires Java 1.8 update 40 or above. I recommend that you download the latest version of Java 8 before starting JavaFX development. A lot of new features such as Alert dialogs were added in JDK 8u40.

The sample JavaFX program below demonstrates the following features,

  • How to use Alert dialog display
  • How to open a new window(stage)
  • How to use the StackPane and VBox layouts
  • How to center align JavaFX layout content
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFXWindowDemo extends Application {

    private String loggedInUser;

    public static void main(String[] args) {

    // JavaFX entry point
    // How to open a new window in JavaFX
    public void start(Stage primaryStage) throws Exception {
        Button btnLogin = new Button();
        btnLogin.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
        // A layout container for UI controls
        StackPane root = new StackPane();

        // Top level container for all view content
        Scene scene = new Scene(root, 300, 250);

        // primaryStage is the main top level window created by platform
        primaryStage.setTitle("JavaFX Demo");

    public void setLoggedInUser(String user) {
        loggedInUser = user;

        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle("Successful login");
        alert.setHeaderText("Successful login");
        String s = user + " logged in!";

    public void showLoginScreen() {
        Stage stage = new Stage();

        VBox box = new VBox();
        box.setPadding(new Insets(10));

        // How to center align content in a layout manager in JavaFX

        Label label = new Label("Enter username and password");

        TextField textUser = new TextField();
        textUser.setPromptText("enter user name");
        TextField textPass = new TextField();
        textPass.setPromptText("enter password");

        Button btnLogin = new Button();

        btnLogin.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                // Assume success always!
                stage.close(); // return to main window

        Scene scene = new Scene(box, 250, 150);

Do you have a question on the above article or do you have a programming problem that you are unable to solve? Please email us.

Comments are closed.