Monday, March 14, 2016

JavaFX example: open new Window and exit all when primary window close

JavaFX example to open new Window, and exit all when primary window close.

package javafxwindow;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

 * @web
public class JavaFXWindow extends Application {
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Open a New Window");
        btn.setOnAction((ActionEvent event) -> {
            Label secondLabel = new Label("Hello");
            StackPane secondaryLayout = new StackPane();
            Scene secondScene = new Scene(secondaryLayout, 200, 100);
            Stage secondStage = new Stage();
            secondStage.setTitle("New Stage");

        StackPane root = new StackPane();
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setOnCloseRequest(e -> Platform.exit());


    public static void main(String[] args) {

1 comment:

  1. You forget to use setOwner like Adobe Air has multiple windows if you close primary window when all second window will exit since your click to primary window. I don't know if JavaFX has setOwned or isOwned from Stage class.