I have tried the below code, it works fine with mouse event, but when I use key event i.e ENTER Key on any button than its not showing the result.
Alert aler
you don't need to set default method, On which ever button you want to enter key
Alert confirmAlert = new Alert(Alert.AlertType.CONFIRMATION, "", ButtonType.YES, ButtonType.NO);
confirmAlert.setHeaderText("Are You Sure Want To Delete ?");
Button bt1 = (Button) confirmAlert.getDialogPane().lookupButton(ButtonType.YES);
Button bt2 = (Button) confirmAlert.getDialogPane().lookupButton(ButtonType.NO);
bt2.addEventHandler(KeyEvent.ANY,(event) -> {
if(event.getCode() == KeyCode.ENTER){
confirmAlert.setResult(ButtonType.NO);
}
});
Optional action = confirmAlert.showAndWait();
if(action.get() == ButtonType.NO){
System.err.println("Action You Want");
}