Confirmation Dialog with JavaFX


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?")


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);
    } catch (IOException ex) {
                .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()

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

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

                public void handle(ActionEvent t) {
                    if (actionHandler != null)

            return this;
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;

    public void handle(Event t) {
        if(button.equals("Yes")) {
            // do yes
        else {
            // do no


and with Dialog you need to do like below

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

Hope this helps.


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.

2/14/2013 1:27:50 PM

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