Problem with Filedownloader

Hello everyone. I’m new to vaadin and I’m trying to download an excel file that I generates. The generation and the download both start by the click of the same button. I wrote the code attached below but when I click on the button the download doesnt start but every other functionality that the button is supposed to perform are performed except for the download.

package com.slotconsulting.airportiq.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


import com.slotconsulting.airportiq.entities.Baggage;
import com.slotconsulting.airportiq.entities.Passenger;

import com.vaadin.server.StreamResource.StreamSource;

import freemarker.core.ReturnInstruction.Return;

public class CheckedInPassengersBackUp {
	private Passenger passenger;
	private static List<Passenger> checkedInPassengers= new ArrayList<Passenger>();
	private StreamSource streamsource;
	private ByteArrayOutputStream xl;
	private String[]  columns = { "First name", "Surname", "PNR", "seat", "Baggage type", "bagagge weight",
			"bag tag", "boardingNumber", "Passport Number", "passportValidity" };
	
	
	public void generatePassengersExcel () throws IOException{
		checkedInPassengers.add(passenger);
		Workbook workbook = new XSSFWorkbook();;
		Sheet sheet = workbook.createSheet("Passengers");
		  Font headerFont = workbook.createFont();
	        headerFont.setBold(true);
	        headerFont.setFontHeightInPoints((short) 14);
	        headerFont.setColor(IndexedColors.RED.getIndex());

	        // Create a CellStyle with the font
	        CellStyle headerCellStyle = workbook.createCellStyle();
	        headerCellStyle.setFont(headerFont);

	        // Create a Row
	        Row headerRow = sheet.createRow(0);

	        // Creating cells
	        for(int i = 0; i < columns.length; i++) {
	            Cell cell = headerRow.createCell(i);
	            cell.setCellValue(columns[i]
);
	            cell.setCellStyle(headerCellStyle);
	        }
	        int rowNum = 0;
	        for( Passenger pass : checkedInPassengers){
	        	
	        	Row row = sheet.createRow(rowNum++);
	        	System.out.println(rowNum);
	        	row.createCell(0)
                .setCellValue(pass.getFirstName());
	        	row.createCell(1)
	        	
                .setCellValue(pass.getSurname());
	        	System.out.println(row.getCell(1).getStringCellValue());
	        	row.createCell(2)
                .setCellValue(pass.getBookingCode());
	        	row.createCell(3)
                .setCellValue(pass.getSeat());
	        	String bagtype="",bagweight="",bagtag="";
	        	for(Baggage bag: pass.getBaggages()){
	        		bagtype+= bag.getType()+"\n";
	        		bagweight+= Double.toString(bag.getWeight())+"\n";
	        		bagtag+= bag.getBagtag()+"\n";
	        	}
	        	row.createCell(4)
                .setCellValue(bagtype);
	        	row.createCell(5)
                .setCellValue(bagweight);
	        	row.createCell(6)
                .setCellValue(bagtag);
	        	row.createCell(7)
                .setCellValue(pass.getBoardingNumber());
	        	row.createCell(8)
                .setCellValue(pass.getPassportNumber());
	        	row.createCell(9)
                .setCellValue(pass.getPassportValidity());
	        	bagtag="";
	        	bagweight="";
	        	bagtype="";
	        }
	        for(int i = 0; i < columns.length; i++) {
	            sheet.autoSizeColumn(i);
	        }
	        System.out.println("siezeeee "+checkedInPassengers.size());
	        ByteArrayOutputStream baos = new ByteArrayOutputStream();
	        workbook.write(baos);
	        baos.close();
	        workbook.close();
	        xl= baos;	
	}
	

	public ByteArrayOutputStream getXl() {
		return xl;
	}


	public void setXl(ByteArrayOutputStream xl) {
		this.xl = xl;
	}


	public Passenger getPassenger() {
		return passenger;
	}


	public void setPassenger(Passenger passenger) {
		this.passenger = passenger;
	}


	public static List<Passenger> getCheckedInPassengers() {
		return checkedInPassengers;
	}


	public static void setCheckedInPassengers(List<Passenger> checkedInPassengers) {
		CheckedInPassengersBackUp.checkedInPassengers = checkedInPassengers;
	}


	public StreamSource getStreamsource() {
		return streamsource;
	}


	public void setStreamsource(StreamSource streamsource) {
		this.streamsource = streamsource;
	}
	public void fileTest(){
		
	}
	
		
	

}

this where I handle the dialog buuton click.

Dialog d = new Dialog();
		
		d.setMessage(fullName);
		d.setInfoComponent(formPresenter.getView());
		d.setWidth(550, Unit.PIXELS);
		d.setHeight(640, Unit.PIXELS);
		d.addButton("Cancel", ValoTheme.BUTTON_BORDERLESS);
		d.addButton("Save", ValoTheme.BUTTON_BORDERLESS_COLORED);
		d.setButtonClickShortcut("Save", KeyCode.ENTER);
		d.setFocus(formPresenter.getFirstFocusable());
		d.show();
		StreamResource resource = new StreamResource(new StreamSource() {

			private static final long serialVersionUID = 1L;

			@Override
			public InputStream getStream() {
					
					try {
						System.out.println("genera stream");
						ByteArrayInputStream bin = new ByteArrayInputStream(ch.getXl().toByteArray());
						bin.close();
						return bin;
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return null;
					}				

			}

		}, item.getBean().getFlight().getDepartureFlightNumber());		
		resource.setMIMEType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		FileDownloader fd = new FileDownloader(resource);
		fd.setFileDownloadResource(resource);
	
		
		
		d.handleResult(new ResultHandler() {
			@Override
			public boolean dialogResult(String result) throws Exception {
				
				if ("Save".equals(result)) {
					ch.setPassenger(item.getBean());
					ch.generatePassengersExcel();
					if (formPresenter.save()) {
						fd.extend(d.getButton("Save"));
					
						if (formPresenter.getBoardingPass().getValue() == true) {
							printBoardingPassAction.execute();
						}
						reloadPassengers();
						return true;
					} else {
						return false;
					}
				}
				return true;
			}
		});

Hi,
Button should be extended before it is clicked.
FileDownloader extensions adds a client side click listener

HTH
Marco

Hey marco. Thanks for the reply.
I already tried that and it works but the problem is that when click the button if I don’t hold the press for about half a second the download doesn’t start.