GWT使用DOM.clone克隆一个小部件

6

我希望能够编写程序克隆一个小部件。我可以使用Dom.clone克隆小部件内的元素,但似乎无法从此克隆的元素创建一个小部件。这可能吗?

        //somewhere in onModuleLoad()...        
    Button button = new Button("Original"); 
    RootPanel.get().add(button);

    //.....later on...
    Element buttonCloneElement = DOM.clone(button.getElement(), true);
    Widget buttonClone;

    buttonClone = new Button(buttonCloneElement);  //FAIL - No such constructor
    buttonClone.setElement(buttonCloneElement);    //FAIL - No such setter method

    //This may work but looks messy to me
    buttonClone.getElement().setInnerHTML(button.getElement().getInnerHTML()); 

    //add the clone to the root panel??
    RootPanel.get().add(buttonClone);

有没有其他克隆Widget的方式?
1个回答

9

buttonClone = Button.wrap(buttonCloneElement)


我有两个ListBox小部件,通过UIBinder @UiField ListBox lstHeaderSort;@UiField ListBox lstFooterSort; ,并且我将其转换为 lstFooterSort = ListBox.wrap(DOM.clone(lstHeaderSort.getElement(), true)); 但是我得到了空指针异常。为什么? - Cataclysm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接