0

I'm new to JavaFx and I'm trying to create a class of a simple confirmation box that determines whether the user really wants to exit or not. It has one function that returns a boolean value, representing if the user clicked "yes" or "no":

public class ConfirmBoxController implements IView {

    public javafx.scene.control.Button yes_BTN;
    public javafx.scene.control.Button no_BTN;

    private volatile boolean answer;

    // Constructors..//

    public boolean confirm(){
        try{
            stage = new Stage();
            FXMLLoader fxmlLoader = new FXMLLoader();
            Parent root = fxmlLoader.load(getClass().getResource("ConfirmBox.fxml").openStream());
            Scene scene = new Scene(root, 250, 140);
            stage.setScene(scene);
            stage.showAndWait();

            return answer;
        }
        catch(Exception E){
            E.printStackTrace();
            return true;
        }
    }

    public void yes() {
        this.answer = true;
        Stage stage = (Stage) yes_BTN.getScene().getWindow();
        stage.close();
    }

    public void no() {
        this.answer = false;
        Stage stage = (Stage) no_BTN.getScene().getWindow();
        stage.close();
    }
}

I tried making "answer" volatile and not, but it didn't change anything.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Tom Dawn
  • 19
  • 3
  • 1) Unless you need synchronization, you can omit "volatile". 2) From the code you posted it is not clear whether there are on click event handlers attached to the buttons. Is this the case (e. g. the methods "yes" and "no" are called)? – Patrick Jun 24 '17 at 11:29
  • `FXMLLoader` creates another instance of the controller. https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian Jun 24 '17 at 12:04
  • But how does the _FXMLoader_ know which class to create an instance of? Because of the _fx:controller_ attribute in the _AnchorPane_ tag in the XML file? But AFAIK this attribute does not necessarily need to be present. – Patrick Jun 24 '17 at 13:14
  • In the end just adding "static" did the trick. – Tom Dawn Jun 24 '17 at 13:51

1 Answers1

0

you can use the javafx build in Dialog or Alert for this functionality here is a tutorial how to use them: http://code.makery.ch/blog/javafx-dialogs-official/

This is what you probably need:

Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Look, a Confirmation Dialog");
alert.setContentText("Are you ok with this?");

Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
    // ... user chose OK
} else {
    // ... user chose CANCEL or closed the dialog
}

Or if you want a Yes/No alert then annd two ButtonTypes

Alert yesNoAlert = new Alert(Alert.AlertType.CONFIRMATION);
yesNoAlert.setTitle("Title");
yesNoAlert.setContentText("Content");
yesNoAlert.setHeaderText("Header");


ButtonType buttonYes = new ButtonType("Yes", ButtonBar.ButtonData.YES);
ButtonType buttonNo = new ButtonType("No" , ButtonBar.ButtonData.NO);

yesNoAlert.getButtonTypes().setAll(buttonYes,buttonNo);

Optional<ButtonType> result = yesNoAlert.showAndWait();
if (result.get() == buttonYes){
    // ...
} else  {
    // ...
}
Sunflame
  • 2,993
  • 4
  • 24
  • 48