简体   繁体   English

GWT getElementbyId到等效的小部件/面板

[英]GWT getElementbyId to equivalent widget / Panel

I'm trying to build a dynamic web app in GWT, when widgets are added to the screen I remember their 'name' by setting the Widget.setId, when I want to replace part of the page, I can find the element in question via DOM.getElementById('name'), and its parent using DOM.getParentElement(), and then remove its children. 我正在尝试在GWT中构建动态Web应用程序,当将小部件添加到屏幕时,我会通过设置Widget.setId记住它们的“名称”,当我要替换页面的一部分时,我可以找到有问题的元素通过DOM.getElementById('name'),其父级使用DOM.getParentElement(),然后删除其子级。

Now I have a com.google.gwt.dom.client.Element object (the parent). 现在,我有了com.google.gwt.dom.client.Element对象(父对象)。 What I want to do is turn this back into a GWT object - in fact it'll be something derived from Panel, so I can add additional Widgets. 我想要做的就是将其转换为GWT对象-实际上,它将是从Panel派生的,因此我可以添加其他小部件。

How do I go from the Element object back to a Panel object ? 我如何从Element对象回到Panel对象?

I totally accept I could be going about this the wrong way, in which case is there a better way? 我完全接受我可能会走错路,在哪种情况下有更好的方法?

I think your approach to remove widgets from the DOM using DOM.getElementById('name') is not the proper one. 我认为您使用DOM.getElementById('name')从DOM中删除小部件的方法不正确。

On your case (I am just figuring out what you do), I would keep Java Objects references instead of accessing to them using the DOM. 就您的情况(我只是弄清楚您的工作),我将保留Java Objects引用,而不是使用DOM访问它们。

For instance: 例如:

HorizontalPanel panel = new HorizontalPanel();
Widget w = new Widget();
//We add the one widget to the panel
panel.add(w);
//One more widget added
w = new Widget();
panel.add(w);
//Now we remove all the widgets from the panel
for(int i = 0; i < panel.getWidgetCount(); i++){
    panel.remove(panel.getWidget(i));
}

UPDATE 更新

Based on your comments, I would propose the following solution. 根据您的意见,我将提出以下解决方案。 I suppose that you are storing widgets on HorizontalPanel , just apply this solution to your concrete case. 我想您将小部件存储在Horizo​​ntalPanel上 ,只需将此解决方案应用于您的具体情况即可。 I propose to use customized class which inherits from HorizontalPanel and add a Map there to store relationship between names and widgets. 我建议使用从Horizo​​ntalPanel继承的自定义类,并在其中添加一个Map来存储名称和小部件之间的关系。

public class MyHorizontalPanel extends HorizontalPanel {

    private Map<String, Widget> widgetsMap;

    public MyHorizontalPanel(){
        super();
        widgetsMap = new HashMap<String, Widget>();
    }

    //We use Map to store the relationship between widget and name
    public void aadWidget(Widget w, String name){
        this.add(w);
        widgetsMap.put(name, w);
    }

    //When we want to delete and just have the name, we can search the key on the map.
    //It is important to remove all references to the widget (panel and map)
    public void removeWidget(String name){
        this.remove(widgetsMap.get(name));
        widgetsMap.remove(name);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM