Default Buttons in Alert API

Daniel Peintner daniel.peintner at
Tue Oct 20 14:33:59 UTC 2020

Hi Robert, all,

Providing an *easy* way to specify the default button in dialogs seems like
a reasonable enhancement to me.


-- Daniel

On Tue, Oct 20, 2020 at 10:26 AM Robert Lichtenberger <
r.lichtenberger at> wrote:

> 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.setTitle("Really");
>   alert.setHeaderText("Danger, Will Robinson");
>   alert.setContentText("This will make the universe collapse. Are you
> sure?");
>   ButtonType yes = new ButtonType("Collapse", ButtonData.OTHER);
>   ButtonType no = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);
>   alert.getButtonTypes().setAll(no, yes);
>   alert.showAndWait();
> 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
> button.
> Would it make sense to add an API (like alert.setDefaultButtonType(...))?
> Best regards,
> Robert

More information about the openjfx-discuss mailing list