单击按钮后删除 GWT 中生成的面板

发布于 2024-07-24 19:18:24 字数 1821 浏览 4 评论 0原文

我正在尝试创建一个也使用 Google Gears 的 Google Web Toolkit (GWT) 应用程序,但每次我尝试删除面板时,都会遇到异常,面板仍保留在那里。

这是我得到的异常的摘录(我只包含了调用堆栈的相关位,其余部分只是下降到下面包含的函数中):

java.lang.AssertionError: A widget that has an existing parent widget may not be added to the detach list
    at com.google.gwt.user.client.ui.RootPanel.detachOnWindowClose(RootPanel.java:122)
    at com.google.gwt.user.client.ui.RootPanel.get(RootPanel.java:197) 

我不确定问题是什么,但我真的不知道就像在他们批准使用 Gears 后将按钮留在那里。

我究竟做错了什么? 或者有什么建议可以让我以不同的方式做到这一点以使其发挥作用?

if(!gearsFactory.hasPermission()) {
    HorizontalPanel rightPanel = new HorizontalPanel();
    rightPanel.getElement().setId("gearsPrompt");
    rightPanel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
    rightPanel.setSpacing(0);
    rightPanel.setHeight("28px");

    InlineLabel enableGearsText = new InlineLabel("Enable Gears for off-line access");
    enableGearsText.getElement().setId("gearsText");
    enableGearsText.addStyleName("titleElement");
    rightPanel.add(enableGearsText);

    final Button gearsButton = new Button("Use Gears");
    gearsButton.getElement().setId("gearsButton");
    gearsButton.addStyleName("titleElement");
    gearsButton.setHeight("24px");

    gearsButton.addClickHandler( new ClickHandler() {
        public void onClick(ClickEvent event) {
            Factory gearsFactory = Factory.getInstance();
            if(gearsFactory != null) {
                if(gearsFactory.getPermission()) {
                    RootPanel gearsPrompt = RootPanel.get("gearsPrompt");
                    gearsPrompt.removeFromParent();
                }
            }
        }
    });

    rightPanel.add(gearsButton);

    RootPanel titleBarRight = RootPanel.get("titleBarRight");
    titleBarRight.add(rightPanel);
}

I am attempting to create a Google Web Toolkit (GWT) application that also uses Google Gears, but every time I try to remove the panel, I get an exception and the panel stays there.

Here is an excerpt from the exception I get (I've only included the relevant bits of the call stack, the rest just descends into the included function below):

java.lang.AssertionError: A widget that has an existing parent widget may not be added to the detach list
    at com.google.gwt.user.client.ui.RootPanel.detachOnWindowClose(RootPanel.java:122)
    at com.google.gwt.user.client.ui.RootPanel.get(RootPanel.java:197) 

I'm not sure what the problem is, but I really don't like leaving the button there after they approve the use of Gears.

What am I doing wrong? Or any suggestions on a different way I could do this to make it work?

if(!gearsFactory.hasPermission()) {
    HorizontalPanel rightPanel = new HorizontalPanel();
    rightPanel.getElement().setId("gearsPrompt");
    rightPanel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
    rightPanel.setSpacing(0);
    rightPanel.setHeight("28px");

    InlineLabel enableGearsText = new InlineLabel("Enable Gears for off-line access");
    enableGearsText.getElement().setId("gearsText");
    enableGearsText.addStyleName("titleElement");
    rightPanel.add(enableGearsText);

    final Button gearsButton = new Button("Use Gears");
    gearsButton.getElement().setId("gearsButton");
    gearsButton.addStyleName("titleElement");
    gearsButton.setHeight("24px");

    gearsButton.addClickHandler( new ClickHandler() {
        public void onClick(ClickEvent event) {
            Factory gearsFactory = Factory.getInstance();
            if(gearsFactory != null) {
                if(gearsFactory.getPermission()) {
                    RootPanel gearsPrompt = RootPanel.get("gearsPrompt");
                    gearsPrompt.removeFromParent();
                }
            }
        }
    });

    rightPanel.add(gearsButton);

    RootPanel titleBarRight = RootPanel.get("titleBarRight");
    titleBarRight.add(rightPanel);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏天碎花小短裙 2024-07-31 19:18:24

我发现的一种解决方案是循环遍历“titleBarRight”面板下的所有小部件,并删除它包含的所有小部件:

if(gearsFactory.getPermission()) {
    RootPanel titleBarRight = RootPanel.get("titleBarRight");
    java.util.Iterator<Widget> itr = titleBarRight.iterator();
    while(itr.hasNext()) {
        itr.next();
        itr.remove();
    }
}

但不知何故,这仍然看起来很老套,而且不完全是“正确的方法”。

One solution I've found is to loop through all of the widgets under the "titleBarRight" panel and remove all widgets it contains:

if(gearsFactory.getPermission()) {
    RootPanel titleBarRight = RootPanel.get("titleBarRight");
    java.util.Iterator<Widget> itr = titleBarRight.iterator();
    while(itr.hasNext()) {
        itr.next();
        itr.remove();
    }
}

But somehow this still seems hacky and not quite the "right way to do it."

浪菊怪哟 2024-07-31 19:18:24

我知道这已经很旧了,但是怎么样...

gearsButton.addClickHandler( new ClickHandler() {
            public void onClick(ClickEvent event) {
                    Factory gearsFactory = Factory.getInstance();
                    if(gearsFactory != null) {
                            if(gearsFactory.getPermission()) {
                                   Button btn=(Button) event.getSource();
                                   btn.removeFromParent();
                            }
                    }
            }
    });

I know this is old, but how about...

gearsButton.addClickHandler( new ClickHandler() {
            public void onClick(ClickEvent event) {
                    Factory gearsFactory = Factory.getInstance();
                    if(gearsFactory != null) {
                            if(gearsFactory.getPermission()) {
                                   Button btn=(Button) event.getSource();
                                   btn.removeFromParent();
                            }
                    }
            }
    });
雨夜星沙 2024-07-31 19:18:24

是否有任何理由使用 RootPanel.get("gearsPrompt").removeFromParent(); 而不是您自己的 rightPanel.removeFromParent();? 参考资料已经在那里了。

Is there any reason for using RootPanel.get("gearsPrompt").removeFromParent(); instead of your own rightPanel.removeFromParent();? The reference is already there.

桃酥萝莉 2024-07-31 19:18:24

你可以这样做:

theParentWidget.remove(index);

第一个孩子对应于0;

You can do :

theParentWidget.remove(index);

and the first child corresponds to 0;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文