How to get FileDownloader to work on the first click of the download button

I’m following the Vaadin sample code but when I do I require a second click to start the file download. Below is my code:

[code]
final StreamResource streamResource = new StreamResource(
() → {
return new ByteArrayInputStream(“hello world”.getBytes());
}, document.getName() + “.txt”);

FileDownloader fileDownloader = new FileDownloader(streamResource);
fileDownloader.extend(getDownloadButton());
[/code]The thing is that the download code is generated on the fly. I saw somewhere that the same included an additional button to first generate but sometimes the data is already generated and you just want to push it through is a file. Is there a way to do this with a single click on the download button?

Although I don’t like the following solution it does works. I’m sure there’s a better solution but right now I simulate the first click with Javascript as suggested in another post here in this forum. If someone has a better solution please let me know…

FileDownloader fileDownloader;
Stream Resource streamResource = createStreamResource();
downloadButton createDownloadButton();
downloadButton.setId("DownloadButtonID");

if(fileDownloader == null)
{
    fileDownloader = new FileDownloader(streamResource);
    fileDownloader.extend(downloadButton);
    // Javascript click so that it works without a second click
    Page.getCurrent().getJavaScript().execute(
        "document.getElementById('DownloadButtonID').click();");
} else {
    fileDownloader.setFileDownloadResource(streamResource);
}