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;
}
You are actually passing a function which returns a
EventHandlerand thus it gets true that it returns aEventHandlerwhich is actually expected from function as a parameter (your function hasreturn 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 anEventHandler. 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.and with Dialog you need to do like below
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.