void (Stage primaryStage) { Button b = new Button(); b.setText("Generate"); b.setOnAction(event -> generate()); StackPane pane = new StackPane(); pane.getChildren().add(b); primaryStage.setScene(new Scene(pane, 300, 200)); primaryStage.show(); Platform.runLater(() -> { generate(); primaryStage.close(); }); }