Search code examples
javajavafxreturn

JavaFX: method and return dosen't seems to work in the proper way


'Morning. I just have a little problem whit java FX. The problem is this: i want to write a code and let a pop up come when use will close up the application via default close button.

package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
    Stage window;
    Button button;
    boolean answer=false;
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        window=primaryStage;
        window.setTitle("Homepage");
        window.setOnCloseRequest(e-> {
            e.consume();
            close();
        });
        StackPane layout = new StackPane();
        layout.getChildren().add(button);
        Scene scene = new Scene(layout, 300,300);
        window.setScene(scene);
        window.show();
    }
private void close(){ answer = ConfirmationBox.display("close app", "'sure about that?");
if(answer) {
    System.out.println(answer);
    window.close();
}
}
    public static void main(String[] args) {
        launch(args);
    }
}

every time i press the default kill app button, a pop up message will follow and ask me if i want to close the app

 public static boolean display(String titolo,String messaggio){
        Stage window = new Stage();
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle(titolo);
        Label label=new Label();
        label.setText(messaggio);


        Button yButton = new Button("yes");
        Button nButton = new Button("no");

        yButton.setOnAction(e-> {
            flag=true;
            window.close();
        });

        nButton.setOnAction(e-> {
            flag=false;
            window.close();
        });

        VBox layout = new VBox(20);
        layout.minHeight(250);
        layout.getChildren().addAll(label,yButton,nButton);
        layout.setAlignment(Pos.CENTER);
        Scene scene = new Scene(layout,300,300);
        window.setScene(scene);
        window.show();

        return flag;

The issue is, when i run for the first time the method, the boolean variable "answer" did not propely save the result, so the close(), can't rly enter inside the if case. how to prevent this? did i miss some javaFX funcionality? i'm new about javaFX and java itself.


Solution

  • Try something like this.

    primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                Alert alert = new Alert(AlertType.CONFIRMATION);
                alert.setTitle("Exit Application");
                alert.setContentText("Do you want to exit?");
    
                Optional<ButtonType> result = alert.showAndWait();
    
                if (result.isPresent() && result.get() != ButtonType.OK) {
                    event.consume();
                }
            }
        });
    }