我正在使用这个对话框类来制作我的项目。当我尝试使用确认对话框时,我必须为 Yes 按钮创建一个 EventHandler,并将其传递给方法调用。我做到了,但是当我单击注销时,我的事件在我单击确认是按钮之前执行。
方法调用
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;
}
对话类:
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;
}