BrowserWindowOpener with Button click event


I am using Vaadin 7.6.5

Trying to open a BrowserWindowOpener as Popup when button is clicked with following code

Button button = new Button(“Open”);

BrowserWindowOpener opener = new BrowserWindowOpener(MyUI.class);
opener.setResource(new FileResource(new File(“G:/New folder/test1470048449513.pdf”)));

it works fine for me , i need to open the BrowserWindowOpener based on some condision(ie i need to do two things check the file first and open BrowserWindowOpener ) when button is clecked so how can i do this code inside buttopn click listener.

Nagaraj RC

if you extend a Button with BrowserWindowOpener then a click handler will be added on the client side
so new window will be opened before your server side click listener is invoked.

If your need is to have a dynamic content generated on button click you can either implement a custom StreamSource and give it to a StreamResource or even extend StreamResource and override getStream or getStreamSource;
then you can initialize your BrowserWindowOpener with this StreamResource.
There are a lot of post regarding this argument on the forum.

Instead if you want to have your listener code inovked before window open take a look at this
in this solution the click listener on client side does nothing and the window is opened at the end of the request generated by clicking the button, but this requires a widgetset recompilation.



I’m here to tell that the below code, using button ClickListener event and setResource from BrowserWindowOpener worked fine for me:

final BrowserWindowOpener browserWindowOpener = new BrowserWindowOpener(resource);
downloadButton.addClickListener(listener -> {
	StreamSource newSource = getStreamSource(simulationStockComboBoxFilter, defaultValue); //my custom StreamSource getter
	StreamResource newResource = new StreamResource(newSource,
			fileNameBeginning + formatter.format( + ".xls");

IMPORTANT: One thing that is mandatory for this to work is to use different filenames for every downloaded file, so that we don’t use the cache.