Java Code Examples for com.vaadin.flow.dom.Element#insertChild()

The following examples show how to use com.vaadin.flow.dom.Element#insertChild() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HasOrderedComponents.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces the component in the container with another one without changing
 * position. This method replaces component with another one is such way
 * that the new component overtakes the position of the old component. If
 * the old component is not in the container, the new component is added to
 * the container. If the both component are already in the container, their
 * positions are swapped. Component attach and detach events should be taken
 * care as with add and remove.
 *
 * @param oldComponent
 *            the old component that will be replaced. Can be
 *            <code>null</code>, which will make the newComponent to be
 *            added to the layout without replacing any other
 *
 * @param newComponent
 *            the new component to be replaced. Can be <code>null</code>,
 *            which will make the oldComponent to be removed from the layout
 *            without adding any other
 */
default void replace(Component oldComponent, Component newComponent) {
    if (oldComponent == null && newComponent == null) {
        // NO-OP
        return;
    }
    if (oldComponent == null) {
        add(newComponent);
    } else if (newComponent == null) {
        remove(oldComponent);
    } else {
        Element element = getElement();
        int oldIndex = element.indexOfChild(oldComponent.getElement());
        int newIndex = element.indexOfChild(newComponent.getElement());
        if (oldIndex >= 0 && newIndex >= 0) {
            element.insertChild(oldIndex, newComponent.getElement());
            element.insertChild(newIndex, oldComponent.getElement());
        } else if (oldIndex >= 0) {
            element.setChild(oldIndex, newComponent.getElement());
        } else {
            add(newComponent);
        }
    }
}
 
Example 2
Source File: AddDivUI.java    From flow with Apache License 2.0 5 votes vote down vote up
private void addDiv() {
    Element bodyElement = getElement();
    Element div = ElementFactory.createDiv("Hello world at "
            + System.currentTimeMillis() + " (" + msgId++ + ")");
    bodyElement.insertChild(0, div);
    if (msgId % 100 == 0) {
        System.out.println("Pushed id " + msgId + " to " + ip);
    }
    // FIXME Enable when remove works
    // while (bodyElement.getChildCount() > 20) {
    // bodyElement.removeChild(20);
    // }
}