Q:

Is it possible to return a variable in javaFX from a close method? Is there a more efficient way to do this?

I would like to run a method that would close a pop up window when an answer is selected or the window is closed manually and then return the value selected (a boolean). Unfortunately, I do not know how I would retreive the data because I initially call the method that displays the query, not the close method.
I have tried many different things but when I try to use a close method I am unable to retrieve the data returned from it. Otherwise, the data returns before the user is able to mutate it. Neither result achieves my goal
This is my method that creates the box. I currently just close the window instead of closing via an alternate closing method.
public class ConfirmBox
{
   static boolean answer;
   public static boolean display(String title, String question) {
         Stage window = new Stage();
        //Block events to other windows
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle(title);
        window.setMinWidth(250);
        //LabelQ
        Label label = new Label();
        label.setText(question);
        //YesBox
        Button yesB= new Button("Yes");
        yesB.getStyleClass().add("button-green");
        yesB.setOnAction(e -> {
            answer=true;
            window.close();
        });
        //NoBox
        Button noB= new Button("No");
        noB.getStyleClass().add("button-red");
        yesB.setOnAction(e -> {
            answer=false;
            window.close();
        });

        window.setOnCloseRequest(e ->{
            e.consume();
            window.close();
        });

        VBox layout = new VBox(10);
        layout.getChildren().addAll(label, yesB, noB);
        layout.setAlignment(Pos.CENTER);

        //Display window and wait for it to be closed before returning
        Scene scene = new Scene(layout, 800, 200);
        scene.getStylesheets().add("styles.css");
        window.setScene(scene);
        window.showAndWait();
        return answer;
    }
}

This is my method that runs everything thus far.
public class Main extends Application
{
    Stage window;
    String gender;
    String name;
    public static void main (String[] args){
        launch (args);
    }
    public void start(Stage primaryStage) throws Exception{
        window= primaryStage;
        window.setTitle("Pokemon Gray");
        GridPane grid= new GridPane();
        grid.setPadding(new Insets(10, 10, 10, 10));
        grid.setVgap(8);
        grid.setHgap(10);
        // Name Label
        Label nameLabel= new Label("UserName");
        GridPane.setConstraints(nameLabel,0, 0);

        //Name input
        TextField nameInput = new TextField();
        GridPane.setConstraints(nameInput,1, 0);

        //GenderLabel
        Label genderLabel= new Label("Gender");
        GridPane.setConstraints(genderLabel,0, 1);

        //Gender Boy
        Button boySelect= new Button("Boy");
        boySelect.getStyleClass().add("button-blue");
        GridPane.setConstraints(boySelect, 1, 1);
        boySelect.setOnAction(e-> gender="Male");

        //Gender Girl
        Button girlSelect= new Button("Girl");
        girlSelect.getStyleClass().add("button-red");
        GridPane.setConstraints(girlSelect, 2, 1);
        girlSelect.setOnAction(e-> gender="Female");

        //Login
        Button saveName= new Button("Save Name");
        GridPane.setConstraints(saveName, 1, 2);
        saveName.setOnAction( e -> {
            boolean valid =verifyText(nameInput.getText());
            if ((valid)&&((gender == "Male")||(gender=="Female"))){
                String qStr= "Are you sure your name is "+nameInput.getText()+" and you are a "+gender+"?";
                boolean confirmed= ConfirmBox.display("Confirmation", qStr );
                if (confirmed == true){
                    name= nameInput.getText();
                }
            }
            else{
                ErrorBox.display("Error", "You must enter a name less than 20 characters long and a gender");
            }
        });

        grid.getChildren().addAll(nameLabel, nameInput, genderLabel, boySelect, girlSelect, saveName);
        Scene scene= new Scene(grid, 1000, 1000);
        scene.getStylesheets().add("styles.css");
        window.setScene(scene);
        window.show();
    }
    public boolean verifyText(String text){
        boolean valid;
        if ((text.length() > 0) && (text.length() <= 20)){
            valid= true;
        }
        else{
            valid=false;
        }
        return valid;
    }
}

I want to be able to run a seperate method that closes it and returns the answer boolean in the ConfirmBox to the Main method. But currently it returns null because it returns immediately.
Edit: It should be noted I currently do not have a close method. I deleted my previous one because it caused many issues.

A:

I think that you can't do that, you need to do another screen like:
If(button.getResult==yes){"charge yesscreen")else{"charge no screen"}

The controllers for differents screens can't interectionate with another screens, you only can call the differents screens from to a controller but you can't pass values with hims, try do something like this I work with javafx with this mentality all screens are independents and you only invoce the differents screens . you can do something like this:
  class first screen{
is a girl?
is a charizar?
is a x)
if(isgirl && is a x...){
call screen x
}else if(ismen&& is a z..){

call screen z
}

{

