GridLayout leaves empty gap after refill

I have a GridLayout (with spacing enabled) which is refilled from time to time. If I refill the grid with more items when it had previously, grid leaves an empty gap in the end. This is a test case:

public class MyVaadinApplication extends Application {
	private Window window;

	@Override
	public void init() {
		window = new Window("Test application");
		setMainWindow(window);

		final GridLayout gridLayout = new GridLayout(2, 1);
		gridLayout.setSpacing(true);

		Button button1 = new Button("Fill with 20 items");
		button1.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				fillGrid(20, gridLayout);
			}
		});

		Button button2 = new Button("Fill with 10 items");
		button2.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				fillGrid(10, gridLayout);
			}
		});

		Label labelBelowGrid = new Label("Label below the grid");
		labelBelowGrid.setSizeUndefined();

		window.addComponent(button1);
		window.addComponent(button2);
		window.addComponent(gridLayout);
		window.addComponent(labelBelowGrid);
	}

	private static void fillGrid(int items, GridLayout gridLayout) {
		gridLayout.removeAllComponents();
		for (int i = 0; i < items; i++) {
			Label item = new Label("TEST");
			item.setSizeUndefined();
			gridLayout.addComponent(item);
		}
	}
}

Steps to reproduce my problem:

  1. add 20 items to the grid
  2. refill the grid with 10 items

after these steps grid leaves an empty gap in the end.

I have attached 2 images to illustrate these two steps.

Looks like a bug for me. Any ideas how to solve this?
12702.jpg
12703.jpg

Please
create a bug report
about this if you cannot find an existing one.

I’ve created a
ticket
. Looks like manually adjusting rows with setRows(int rows) solves the problem, but in my opinion GridLayout should do it itself.