Dialog strange behavior

Hi everyone,

I have a question about the Dialog component.

I have a dialog with one Button, called Cancel. The only thing it does is to close the dialog. Which is working as expected.

Button btnCancel = new Button("Cancel");
btnCancel.addClickListener(clickEvent ->{
	dialog.close();
});

I have also added dialog close listener, which for test purposes just prints “closed”

dialog.addDialogCloseActionListener(event -> {
	System.out.println("closed");
});

The strange behavior is when i click outside of the dialog or press Esc the close action is triggered(prints closed), but the dialog does not close. And if i press my cancel button - the dialog is closed, but the close event is not triggered.

Is this the expected behavior?

I am currently on version 14.1.3

Thanks,
Vladislav

Hello, Vladislav.

That’s by design. According to the javadoc for the Dialog#addDialogCloseActionListener, it says:

Add a listener that controls whether the dialog should be closed or not.
The listener is informed when the user wants to close the dialog by
clicking outside the dialog, or by pressing escape. Then you can decide
whether to close or to keep opened the dialog.
It means that dialog won’t
be closed automatically unless you call {@link #close()} method
explicitly in the listener implementation.
NOTE: adding this listener changes behavior of the dialog. Dialog is
closed automatically in case there are no any close listeners. And the
{@link #close()} method should be called explicitly to close the dialog
in case there are close listeners.

That way, it’s up to the server to decide whether or not the dialog will be closed. Meanwhile the action triggered by the button (dialog.close()) won’t dispatch the same event, so that’s why nothing is printed on the console.

Diego Cardoso:
Hello, Vladislav.

That’s by design. According to the javadoc for the Dialog#addDialogCloseActionListener, it says:

Add a listener that controls whether the dialog should be closed or not.
The listener is informed when the user wants to close the dialog by
clicking outside the dialog, or by pressing escape. Then you can decide
whether to close or to keep opened the dialog.
It means that dialog won’t
be closed automatically unless you call {@link #close()} method
explicitly in the listener implementation.
NOTE: adding this listener changes behavior of the dialog. Dialog is
closed automatically in case there are no any close listeners. And the
{@link #close()} method should be called explicitly to close the dialog
in case there are close listeners.

That way, it’s up to the server to decide whether or not the dialog will be closed. Meanwhile the action triggered by the button (dialog.close()) won’t dispatch the same event, so that’s why nothing is printed on the console.

what is the event dispatch by button(dialog.close()) ?

José Hamilton:

Diego Cardoso:
Hello, Vladislav.

That’s by design. According to the javadoc for the Dialog#addDialogCloseActionListener, it says:

Add a listener that controls whether the dialog should be closed or not.
The listener is informed when the user wants to close the dialog by
clicking outside the dialog, or by pressing escape. Then you can decide
whether to close or to keep opened the dialog.
It means that dialog won’t
be closed automatically unless you call {@link #close()} method
explicitly in the listener implementation.
NOTE: adding this listener changes behavior of the dialog. Dialog is
closed automatically in case there are no any close listeners. And the
{@link #close()} method should be called explicitly to close the dialog
in case there are close listeners.

That way, it’s up to the server to decide whether or not the dialog will be closed. Meanwhile the action triggered by the button (dialog.close()) won’t dispatch the same event, so that’s why nothing is printed on the console.

what is the event dispatch by button(dialog.close()) ?

There’s no event like that at the moment, although there probably should be. You can work around the issue by extending Dialog; there’s some discussion and a description of a workaround here: https://github.com/vaadin/vaadin-dialog-flow/issues/95

I like to use a slightly different approach for something to happen whenever a dialog is closed (no matter how):

In the constructor of your own Dialog class (… extends Dialog) you add this line:

addListener(DialogCloseActionEvent.class, event -> this.close());

This will make any esc or outside-click call the dialogs close() method. Now we can override that close method and add the code that should always be run at closure:

@Override
public void close(){
    System.out.println("closed"); // this will always be printed now, no matter *how* you close the dialog.
    super.close();
}