Search code examples
layoutcollectionscomponentsvaadinvaadin8

Get collection of components in a layout in Vaadin 8


Is there a way to get a collection of all the components contained in a Layout in Vaadin 8?

I have a GridLayout with a bunch of TextField objects. I'd like to set all of them to have 100% width. Rather than hand-code each explicitly, I'd rather write a soft-coded loop that looks through all the widgets in the layout.


Solution

  • All Layouts in Vaadin 8, that can host multiple components (like GridLayout) implement HasComponents Interface

    https://vaadin.com/download/release/8.4/8.4.4/docs/api/com/vaadin/ui/HasComponents.html

    The interface defines Iterator, which can be used to go thru the components in the layout. I think it fits perfectly your purpose.

    Since Java 8 is supported, Iterable supports also forEach(..).