Child window size tied to contained table size

I am trying to create a set of child windows that will pop up in the center of the screen (not simultaneously, of course). Each will contain a table with a single column. The column is a list of items, and the number of items varies from as few as four to as many as twenty or more. The string length of an item varies considerably, too, from as few as five characters to fifty or more.

I want to create a reusable child window to hold a table with the elements of a given list. I want a minimum size for the window (say 200px wide by 150px tall) and a maximum size (say 400px wide and 300px tall). Between these two extremes I want the window to resize based on the number of elements and the width of the largest element in the table.

Even setting aside the max and min sizes, I’ve been completely unable to get the window to resize with the table. I’ve tried every combination of setHeight, setWidth, setSizeFull, setSizeUndefined, and setExpandRatio that I can think of, and in every different order. Unless I set the table height to a specific pixel value, it always seems to fix its height at 15 rows, whether there are actually 5 rows or 50. Similarly, whether I set the table to sizeFull or sizeUndefined, it seems to size itself by the width of the caption. See the attached image for an example.

I’d post code, but what code should I post? I’ve tried it 20 different ways and none worked, so anything I posted would be arbitrary.

Is there anyone who knows if this is possible and, if so, how to make it happen? I’d be happy even without min/max size limits – just a window that sizes according to the contained table would be enough!

Thanks much for any help!
11333.png

Unfortunately min-max sizes can not be done, but otherwise you could do it with something like:

public class PopupTable extends Window {

	public Table table = new Table();
	
	PopupTable(String caption, Window parent, Container data) {
		super(caption);
		VerticalLayout vl = new VerticalLayout();
		setContent(vl);
		table.setContainerDataSource(data);
		parent.addWindow(this);
		vl.setSizeUndefined();
		vl.addComponent(table);
		center();
	}

	@Override
	protected void close() {
		table.setContainerDataSource(null);
		super.close();
	}
	
}

Thanks! That was very helpful for getting the table to fit to the window. I ended up adding a panel for nice margins.

The min/max issue is actually pretty easy. I just added methods to calculate the height in rows and convert rows to pixels. Then I compare to min and max sizes and set the table height accordingly. My method allows me to pass in a rowHeight parameter, so if I figure out a way to calculate that later I can set it automatically.

The width is a little trickier. I get the length of the longest line in characters, multiply by an average character width in pixels, add a few extra character widths for good measure, and set the table width accordingly (after comparing to min and max sizes).

Works like a charm…
11335.png