A label doesn't show up in two layouts

Hi all,
I was expecting that a single label would show up in two different layouts if I want it to. But it doesn’t, see the code below.


	protected void init(VaadinRequest request) {
		final VerticalLayout layout = new VerticalLayout();
		layout.setMargin(true);
		setContent(layout);

		Label label = new Label("Will this show twice?");
		CssLayout l1 = new CssLayout();
		l1.addComponent(label); // this one doesn't show up
		layout.addComponent(l1);

		CssLayout l2 = new CssLayout();
		l2.addComponent(label);
		layout.addComponent(l2);
	}

With this code only one label shows up on my screen. I think this is a bug but please correct me if I’m wrong. Occurs at least with 7.x series.
Thanks & cheers,
Ville

All components including Label can have only one parent container.

Syam is right, a component instance is only allowed to reside in one parent container at a time. Adding a component to a parent will implicitly remove it from any previous parent container.

You could create a property of the content of your label and bind that to the labels if you wish for them to always contain the exact same string of text.

The component hierarchy is a tree, just like the DOM hierarchy on the client side is a tree. I don’t know of any UI library/framework where components could have multiple parents (making the hierarchy a DAG - directed acyclic graph). It might certainly allow some nice things but would also significantly increase the all-around complexity of the system.

On the other hand, why is no exception thrown if trying to add a component to another parent without removing it first?

Indeed. Probably because someone once thought it’s “convenient” the way it is. And now we can’t change it without breaking
a lot
of stuff.