I'm working on a project in Vaadin 7. In that I need to parse over all the components in a Layout and find a component I need.
The above is the pictorial representation of my layout.
I'm dynamically creating the green coloured Vertical layout inside blue coloured Vertical layout. Since I'm creating them dynamically, I can't have any instance for those dynamically created things. But, I have unique ID's for all the components.
Now I need to find a Combobox using the Id. I donno how to parse in to the combobox from the Blue coloured vertical layout.
All I have is an instance of the blue coloured vertical layout and Id's for combobox. And, I can have ID's for green and red layouts too if needed.
I need something like this, But stuck..
Iterator<Component> iterate = blueMainLayout.iterator();
Combobox cb;
while (iterate.hasNext()) {
Component c = (Component) iterate.next();
cb = (Combobox) blueMainLayout.....;
if (cb.getId().equals(something.getId())) {
// do my job
}
}
You have to check component recursively.
class FindComponent {
public Component findById(HasComponents root, String id) {
System.out.println("findById called on " + root);
Iterator<Component> iterate = root.iterator();
while (iterate.hasNext()) {
Component c = iterate.next();
if (id.equals(c.getId())) {
return c;
}
if (c instanceof HasComponents) {
Component cc = findById((HasComponents) c, id);
if (cc != null)
return cc;
}
}
return null;
}
}
FindComponent fc = new FindComponent();
Component myComponent = fc.findById(blueMainLayout, "azerty");
Hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With