Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vaadin : How to get Element by ID?

Tags:

vaadin

vaadin7

How to get HTML Elmement (or DOM) in Vaadin ?
In GWT I can use as DOM.getElementById("myId"); I can set id attribute on my Vaadin components by setId() method. For example:

    Button button = new Button("Say Hello");
    button.setId("myButton");

So, how can I retrieve this DOM Element in Vaadin ?

like image 842
Cataclysm Avatar asked Feb 13 '23 10:02

Cataclysm


1 Answers

You can use this:

public static Component findComponentById(HasComponents root, String id) {
    for (Component child : root) {
        if (id.equals(child.getId())) {
            return child; // found it!
        } else if (child instanceof HasComponents) { // recursively go through all children that themselves have children
            Component result = findComponentById((HasComponents) child, id);
            if (result != null) {
                return result;
            }
        }
    }
    return null; // none was found
}

Source: https://vaadin.com/forum/#!/thread/3199995/3199994

like image 166
Topera Avatar answered Feb 24 '23 05:02

Topera