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
Here how i solved it. it works fine below is my confirm alert function.
public static boolean confirmAlert(String title, String msg){
ButtonType buttonTypeYes = new ButtonType("Yes", ButtonBar.ButtonData.OK_DONE);
ButtonType buttonTypeNo = new ButtonType("No",ButtonBar.ButtonData.CANCEL_CLOSE);
Alert alert = new Alert(Alert.AlertType.NONE, msg,buttonTypeNo,buttonTypeYes);
alert.setTitle(title);
Button button1 = (Button) alert.getDialogPane().lookupButton(buttonTypeYes);
button1.setDefaultButton(false); //***set default to false***
Button button2 = (Button) alert.getDialogPane().lookupButton(buttonTypeNo);
button1.setOnKeyReleased(event -> {
if(event.getCode() == KeyCode.ENTER)
alert.setResult(buttonTypeYes);
});
button2.setOnKeyReleased(event -> {
if(event.getCode() == KeyCode.ENTER)
alert.setResult(buttonTypeNo);
});
alert.showAndWait();
return alert.getResult()==buttonTypeYes;
}
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 <ButtonType> action = confirmAlert.showAndWait();
if(action.get() == ButtonType.NO){
System.err.println("Action You Want");
}
I don't know if this is not a kind of too much of a workaround, but at least it works:
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
[...]
ButtonType buttonTypeTwo = new ButtonType("Two");
ButtonType buttonTypeThree = new ButtonType("Three");
alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeThree);
//Create a button for every ButtonType you add to your alert and give it a Eventhandler
Button button1 = (Button) alert.getDialogPane().lookupButton(buttonTypeOne);
Button button2 = (Button) alert.getDialogPane().lookupButton(buttonTypeTwo);
Button button3 = (Button) alert.getDialogPane().lookupButton(buttonTypeThree);
button1.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if(event.getCode() == KeyCode.ENTER)
alert.setResult(buttonTypeOne);
}
});
button2.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if(event.getCode() == KeyCode.ENTER)
alert.setResult(buttonTypeTwo);
}
});
button3.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if(event.getCode() == KeyCode.ENTER)
alert.setResult(buttonTypeThree);
}
});
//go ahead with your code
Optional<ButtonType> result = alert.showAndWait();
[...]
You just create some Buttons and assigned them the actual buttons on your alert. In the next Step you can give every button an EventHandler which just (In this example) checks - when any key is released - if the key was ENTER and set the result.
I guess there are better solutions for this. But it's the easiest way which comes to my mind currently. Hope it helps you.
I do not recommend making all buttons respond to enter, as that is counter to how most UI dialogs work.
Normally, a button with focus will fire when you press space, not enter. There are however special buttons that will activate on specific keys: A default button will fire on enter and a cancel button will fire on esc. Usually you will have only one of each of these special types of buttons in your dialog, so that they can be fired via the special keyboard accelerator, regardless of which button currently has focus.
Additionally, different desktop OS systems have different standards on placement of default and cancel buttons in a dialog system. This is to assist the user in easily finding these special buttons in any dialog. The JavaFX dialog system implements some logic internally for locating buttons in dialogs where the user would expect to see them across different desktop operating systems.
Let's say you want the button types from your example to be defined as default or cancel buttons and placed in the correct position for such buttons for your OS, then you can do as below:
ButtonType buttonTypeTwo = new ButtonType(
"Two",
ButtonBar.ButtonData.OK_DONE
);
ButtonType buttonTypeThree = new ButtonType(
"Three",
ButtonBar.ButtonData.CANCEL_CLOSE
);
Note the JavaFX system has automatically changed the position of the buttons and some of the color highlighting. When the user presses enter, then "Two" will fire, when the user presses esc, then "Three" will fire. If you run the same code on Windows or Linux, likely the buttons will be positioned differently, according to whatever button positioning standard is used for those OSes.
If you don't want JavaFX to reposition your buttons according to OS standards, but you want them to still respond to enter and esc keys, then you can lookup the buttons and directly modify the button attributes like below:
Button buttonTwo = (Button) alert.getDialogPane().lookupButton(buttonTypeTwo);
buttonTwo.setDefaultButton(true);
Button buttonThree = (Button) alert.getDialogPane().lookupButton(buttonTypeThree);
buttonThree.setCancelButton(true);
I recommend letting JavaFX appropriately position buttons of specific types rather than performing lookups as above.
I also recommend setting at least a CANCEL_CLOSE button or OK_DONE button in your JavaFX Alert, otherwise the user may have a difficult time actually closing the alert as the dialog will probably not respond to key presses as the user expects.