vaadin flow ConfirmDialog addCancelListener

Viewed 59

I have the following two classes.

the first one extends ConfirmDialog

 public void showDialogFromList(ArrayList<EntityMessage> listMessage)
 { 
  .........    
 this.setText(new Html("<div>"+message+"</div>").getElement())
 
 Image imgOk = new Image(ICO_OK16,"ok");       
 Button buttonYes = new Button("συνέχεια");
 buttonYes.setIcon(imgOk);
 this.setConfirmButton(buttonYes);
 this.addConfirmListener(event -> {
    
     this.close();
 });


 
 Image imgCancel = new Image(ICO_CANCEL16,"cancel");
 Button buttonNo = new Button("άκυρο");
 buttonNo.setIcon(imgCancel);
 this.setCancelButton(buttonNo);

 

 this.open();
   }

and is called by this part

 UtilsGui utilsGui = new UtilsGui();
          
         utilsGui.showDialogFromList(listMessages);
         utilsGui.addCancelListener( e->{
             System.out.println(" ------A checkForListMessages   utilsGui.showMessageFromList  return   intListMessages:"+utilsGui.getDialogResult());  
             utilsGui.close();
                    });

Unfortunately I cannot get the message from System.out.println appear when the utilsGui.addCancelListener should run. I am using version 20.0.6 Thank you in advance.

1 Answers

The ConfirmDialog offers built-in buttons (confirm, cancel) that are configured to close the dialog and fire events. If you don't use the default buttons, but instead use your own components for these buttons (as in your case), it's your responsibility to take care of both of those actions yourself.

Here's a working example of an extension of the ConfirmDialog similar to yours:

public static class TestDialog extends ConfirmDialog {
    public void showDialog(String message) {
        
        // build the content of the dialog
        add(new H1(message));

        // supply a custom cancel component
        setCancelButton(new Button("Cancel", e -> {
            close();
            fireEvent(new CancelEvent(this, true));
        }));
    
        // supply a custom confirm component
        setConfirmButton(new Button("Confirm", e -> {
            close();
            fireEvent(new ConfirmEvent(this, true));
        }));

        open();
    }
}
Related