Directory

← Back

ConfirmDialog

A versatile confirmation dialog for Vaadin

Author

Contributors

Rating

Ever needed a present a confirmation dialog in a Vaadin application?

Here is a way to do that nicely. The ConfirmDialog add-on introduces a configurable way of requesting user confirmation for a button click or just before some important operation.

Defaults like size calculation, keyboard bindings and styling are all there, but of course you can provide your own. Take a look at the demo application for sample code and use cases.

Supports both two-way (yes,no) and three-way (yes,no,cancel) confirmation. The three-way support was added in version 2.1.x. If you only need a "monolog box", please use the Vaadin's own showNotification method.

This is a server-side-only component, so no need to recompile the widgetset.

Sample code

ConfirmDialog.show(win, "Please Confirm:", "Are you really sure?",
        "I am", "Not quite", new ConfirmDialog.Listener() {

            public void onClose(ConfirmDialog dialog) {
                if (dialog.isConfirmed()) {
                    // Confirmed to continue
                    feedback(dialog.isConfirmed());
                } else {
                    // User did not confirm
                    feedback(dialog.isConfirmed());
                }
            }
        });
// Customization via the factory. This makes all subsequent dialog
// calls to use this application-wide.
ConfirmDialog.Factory df = new DefaultConfirmDialogFactory() {

    // We change the default order of the buttons
    @Override
    public ConfirmDialog create(String caption, String message,
            String okCaption, String cancelCaption) {
        ConfirmDialog d = super.create(caption, message, okCaption,
                cancelCaption);

        // Find the buttons and change the order
        Button ok = d.getOkButton();
        HorizontalLayout buttons = (HorizontalLayout) ok.getParent();
        buttons.removeComponent(ok);
        buttons.addComponent(ok, 1);
        buttons.setComponentAlignment(ok, Alignment.MIDDLE_RIGHT);

        // Change the default
        Button cancel = d.getCancelButton();
        cancel.setStyleName(Reindeer.BUTTON_DEFAULT);
        ok.removeStyleName(Reindeer.BUTTON_DEFAULT);
        cancel.focus();

        return d;
    }

};
ConfirmDialog.setFactory(df);

// Now we can confirm the standard way
ConfirmDialog.show(getMainWindow(), "Really delete all?",
        new ConfirmDialog.Listener() {

            public void onClose(ConfirmDialog dialog) {
                if (dialog.isConfirmed()) {
                    // Confirmed to continue
                    feedback(dialog.isConfirmed());
                } else {
                    // User did not confirm
                    feedback(dialog.isConfirmed());
                }
            }
        });

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

  • Added API for getting/setting buttons and fixed regression in 3.1.2.
  • Fixed extra margin issues. Thanks @BrunoEberhard
  • Fixed long message wrap in Valo. Thanks @knoobie
Released
2017-06-13
Maturity
CERTIFIED
License
Apache License 2.0

Compatibility

Framework
Vaadin 8.0+
Vaadin 7.0+ in 2.1.3
Vaadin 6.3+ in 1.2.1
Browser
Internet Explorer
Internet Explorer
Internet Explorer
Firefox
Opera
Google Chrome
Internet Explorer
iOS Browser
Android Browser
Internet Explorer
Microsoft Edge
Online