Default Buttons in Alert API
r.lichtenberger at gmail.com
Tue Oct 20 08:23:30 UTC 2020
The Alert API allows to specify Button text, which is a good thing, since
it is good UI design to label your buttons with the action that will happen
when you click them.
There is however no easy way to specify the default button of such a
dialog, which is bad.
Consider this confirmation dialog:
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setHeaderText("Danger, Will Robinson");
alert.setContentText("This will make the universe collapse. Are you
ButtonType yes = new ButtonType("Collapse", ButtonData.OTHER);
ButtonType no = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);
It will make the "Collapse" option the default one, which is bad.
I would expect sth. along the lines of alert.setDefaultButtonType(no) but
no such API exists.
Instead there are (AFAIK) two ways to work around this:
1) using DialogPane.lookupButton to lookup each button and calling
setDefaultButton(true/false); this seems clumsy to me and also there is no
guarantee for this to work (i.e. Alert could call setDefaultButton itself
during showAndWait(), overriding what has been done before)
2) using ButtonData values that will produce the desired effect, e.g.
ButtonType no = new ButtonType("Cancel", ButtonData.OK_DONE);
but this is semantically wrong, the Cancel button is not really an OK_DONE
Would it make sense to add an API (like alert.setDefaultButtonType(...))?
More information about the openjfx-discuss