Confirmation Dialog with JavaFX


Question

I'm using this Dialog Class to make my project. When I try to use the confirmation Dialog I have to create a EventHandler for the Yes button, and pass it in the method call. I did it, but when I click to logout, my event is executed before I click in my confirmation yes button.

Method call

public void btnSairClicked(ActionEvent event) {

    Dialog.buildConfirmation("Confirmar", "Deseja realmente sair?")
            .addYesButton(actionPerformed(event))
            .addNoButton(null)
            .build()
            .show();

}

private EventHandler actionPerformed(ActionEvent event) {
    String loginUrl = "http://" + Constants.TARGET_HOST + ":" + Constants.TARGET_PORT + Constants.TARGET_SERVICE_LOGOUT_PATH;
    try {
        JSONObject json = HttpUtil.getJSON(false, loginUrl, null, null, null);
        loginManager.logout();
    } catch (IOException ex) {
        Logger.getLogger(MainViewController.class
                .getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

Dialog class:

public static Builder buildConfirmation(String title, String message) {
        return buildConfirmation(title, message, null);
    }

public static Builder buildConfirmation(String title, String message, Window owner) {
        return new Builder()
            .create()
            .setOwner(owner)
            .setTitle(title)
            .setConfirmationIcon()
            .setMessage(message);
    }

public Builder addYesButton(EventHandler actionHandler) {
            return addConfirmationButton("Sim", actionHandler);
        }

protected Builder addConfirmationButton(String buttonCaption, final EventHandler actionHandler) {
            Button confirmationButton = new Button(buttonCaption);
            confirmationButton.setMinWidth(BUTTON_WIDTH);
            confirmationButton.setOnAction(new EventHandler<ActionEvent>() {

                public void handle(ActionEvent t) {
                    stage.close();
                    if (actionHandler != null)
                        actionHandler.handle(t);
                }
            });

            stage.buttonsPanel.getChildren().add(confirmationButton);
            return this;
        }
1
3
2/12/2013 3:15:14 PM

Accepted Answer

You are actually passing a function which returns a EventHandler and thus it gets true that it returns a EventHandler which is actually expected from function as a parameter (your function has return null;, I really don't have any idea about that). That is a function as well which gets executed as you assign (that's basically a call). So you need to create a perfect handler first not just a function which returns an EventHandler. Now your click event can handle things..... You can debug it by putting a break point at that Dialog line and check the things I said.

class MyHandler implements EventHandler<Event> {

    private String button = "";

    public MyHandler(String s) {
        this.button = s;
    }

    @Override
    public void handle(Event t) {
        if(button.equals("Yes")) {
            // do yes
            System.out.println("Yes");
        }
        else {
            // do no
            System.out.println("No");
        }
    }

}

and with Dialog you need to do like below

 Dialog.buildConfirmation("Confirmar", "Deseja realmente sair?")
                    .addYesButton(new MyHandler("Yes"))
                    .addNoButton(new MyHandler("No"))
                    .build()
                    .show();

Hope this helps.

EDIT :

I have created a constructor and it accepts String pass yes to perform yes based operations and so on. Check for the string and perform your operations.

3
2/14/2013 1:27:50 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon