0

I'm trying to write a custom eventhandler based on the alert that my application creates when you press a button. This is what I'm trying to extract (see below).

view.getStart().setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
            alert.setHeaderText("This will reset the game.");
            alert.setContentText("Are you sure?");
            alert.setTitle("Warning");
            alert.getButtonTypes().clear();
            ButtonType no = new ButtonType("No");
            ButtonType yes = new ButtonType("Yes");
            alert.getButtonTypes().addAll(no, yes);
            alert.showAndWait();
            if (alert.getResult() == null || alert.getResult().equals(no)) {
                event.consume();
            }
            if (alert.getResult().equals(yes)) {
                view.getTextField();
                view.getScene().getWindow().hide();
            }
        }
    });

I know how to make the handler a standalone class and then use that instead of new EventHandler, but I've no idea how to get the variables: alert.getResult(), alert.getButtonType() & alert.getEvent.consume();

Note: I'm working with model view presenter and this is currently in my view class.

m4t5k4
  • 55
  • 9

1 Answers1

0

Just make the EventHandler a top level class. The only thing it seems to need access to is the view. (You might be able to get away with giving it a reference to the window, however it's likely you want to register the event handler before the view is added to a window.)

Assuming view is from a class called View (just change that as needed):

public class CloseRequestHandler implements EventHandler<ActionEvent> {

    private final View view ;

    public CloseRequestHandler(View view) {
        this.view = view ;
    }

    @Override
    public void handle(ActionEvent event) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText("This will reset the game.");
        alert.setContentText("Are you sure?");
        alert.setTitle("Warning");
        alert.getButtonTypes().clear();
        ButtonType no = new ButtonType("No");
        ButtonType yes = new ButtonType("Yes");
        alert.getButtonTypes().addAll(no, yes);
        alert.showAndWait();
        if (alert.getResult() == null || alert.getResult().equals(no)) {
            event.consume();
        }
        if (alert.getResult().equals(yes)) {

            // this line of code doesn't do anything, btw:
            view.getTextField() ;

            view.getScene().getWindow().hide();
        }
    }
}

And then all you need is

view.getStart().setOnAction(new CloseRequestHandler(view));
James_D
  • 201,275
  • 16
  • 291
  • 322
  • view.getTextField() is for setting a level in the game (I need to work this out later) – m4t5k4 Mar 05 '17 at 19:32
  • @m4t5k4 But it doesn't do anything. You just discard the result of the method call. – James_D Mar 05 '17 at 19:38
  • In the other question you advised to implement the EventHandler interface, why are we extending here instead of implementing? Sorry if this is a dumb question. – m4t5k4 Mar 05 '17 at 19:46
  • What do you mean? If the alert result is "yes" the second if will be executed right? – m4t5k4 Mar 05 '17 at 19:54
  • Yes, but what I am trying to tell you is that just calling a `getXXX()` method does not do anything, unless you do something with the result. All it does (or at least all it should do) is return a value. Since you don't do anything with the returned value, it's redundant. – James_D Mar 05 '17 at 19:57
  • Yup, I need to give the result to a model class so it can change the difficulty of the game. – m4t5k4 Mar 05 '17 at 20:00