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) { launch(args); } // JavaFX entry point // How to open a new window in JavaFX @Override public void start(Stage primaryStage) throws Exception { Button btnLogin = new Button(); btnLogin.setText("Login"); btnLogin.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { showLoginScreen(); } }); // A layout container for UI controls StackPane root = new StackPane(); root.getChildren().add(btnLogin); // 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"); primaryStage.setScene(scene); primaryStage.show(); } 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!"; alert.setContentText(s); alert.show(); } 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 box.setAlignment(Pos.CENTER); 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.setText("Login"); btnLogin.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { // Assume success always! setLoggedInUser(textUser.getText()); stage.close(); // return to main window } }); box.getChildren().add(label); box.getChildren().add(textUser); box.getChildren().add(textPass); box.getChildren().add(btnLogin); Scene scene = new Scene(box, 250, 150); stage.setScene(scene); stage.show(); } }