The solution to the second click requirement for downloading the file is the button's listener. Like in my case, i have the click listener of button in which FileDownloader extends button. But it should be without listener only as FileDownloader has its own mechanism to handle the listener actions.
Here, the first button click gets handled by the clickListener and only in that, fileDownloader extends download button which holds all the functionality of downloading the file and this functionality occurs only when click event goes through the FileDownloader. so for the next time click goes through the FileDownloader as now its extending the button.
public static Button getDownloadButton(String fileName, String fileAsString, String caption) {
// caption
Button dwnButton = new Button(caption, VaadinIcons.DOWNLOAD);
dwnButton.addClickListener(listener -> {
StreamResource resource = createResource(fileName, fileAsString);
FileDownloader fileDownloader = new FileDownloader(resource);
fileDownloader.extend(dwnButton);
});
return dwnButton;
}
Here, fileDownloader already extending the button and it has all the stream resources. So on the first click only downloading gets invoked.
public static Button getDownloadButton(String fileName, String fileAsString, String caption) {
// caption
Button dwnButton = new Button(caption, VaadinIcons.DOWNLOAD);
StreamResource resource = createResource(fileName, fileAsString);
FileDownloader fileDownloader = new FileDownloader(resource);
fileDownloader.extend(dwnButton);
return dwnButton;
}