Viritin DownloadButton cannot be triggered programmatically or by shortcuts

Hello everyone :slight_smile:

Thanks for reading my post. In my Vaadin8-Application, I use a Viritin-DownloadButton to allow the user to generate and download a report.

The button is very useful, but - unlike my other buttons - it cannot be clicked via a shortcut or programmatically. Only by focusing it and pressing enter or by actually clicking it with the mouse can I start a download.

DownloadButton pdfButton = new DownloadButton();
//[...]

pdfButton.setClickShortcut(KeyCode.P); //Doesn't work...
//[...]

pdfButton.click();//Does not work

This applies to Viritin 2.5 and 2.9. Do you folks have any idea why that might be? And do you know of any ways to trigger the download programmatically? I need that in an eventListener of a Grid, where a doubleclick is supposed to start the download.

Thanks in advance for your help.

Downloads can only be started directly as a result of a user action because of browser security restrictions. This means that if you have a server-side event listener reacting to something, the download action is disconnected from the original user event and thus blocked by the browser. You’d need to write the event handler entirely in JavaScript to make it work.

Thanks a lot for your quick answer.

Do you know how to click the Button from JavaScript, though? Since it’s a div and not a normal button…

I imagine something like

pdfButton.setId("pdfButtonId");
//[...]

//In the eventListener
Page.getCurrent().getJavaScript().execute(
	"document.getElementById('pdfButtonId').click()");

but probably “click()” isn’t really it, hm?

Here’s the html of the button:

<div tabindex="0" role="button" class="v-button v-widget v-has-width" style="width: 100px;" id="pdfButtonId">
	<span class="v-button-wrap"><span class="v-button-caption">PDF</span></span>
</div>

Thanks again for your help,

In a Vaadin 8 application, it might make most sense to do a GWT add-on.