gpt4 book ai didi

java - 如何组合多个基于 uiBinder 的小部件?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:46:30 24 4
gpt4 key购买 nike

我需要在特定位置将 [数量] 个基于 uiBinder 的小部件插入另一个小部件。插入的小部件的布局有些复杂,所以我试图用 HTML 定义它。

referencePanel.add(...) 失败并出现 java.lang.IllegalStateException:此小部件的父级未实现 HasWidgets。不知道它对哪个小部件的父级不满意 - innerPanel 或 referencePanel。

如果将 ReferenceUI 对象添加到 RootPanel,然后将其添加到页面底部。但是如果它先被添加到 RootPanel 中,然后在添加到 referencePanel 时出现 JavaScriptException 代码 3 (HIERARCHY_REQUEST_ERR)。

有什么建议吗?

public class AppUIDemo extends Composite {
@UiTemplate("AppUIDemo.ui.xml")
interface AppUIDemoUiBinder extends UiBinder<Widget, AppUIDemo> {
}

@UiTemplate("ReferenceUI.ui.xml")
interface ReferenceUIUiBinder extends
UiBinder<Widget, ReferenceUI> {
}

private static AppUIDemoUiBinder uiBinder = GWT
.create(AppUIDemoUiBinder.class);
private static ReferenceUIUiBinder refUIBinder = GWT
.create(ReferenceUIUiBinder.class);

@UiField
FlowPanel referencePanel;

public AppUIDemo() {
initWidget(uiBinder.createAndBindUi(this));
ReferenceUI reference = new ReferenceUI(refUIBinder);

HTMLPanel innerPanel = reference.getRefPanel();
innerPanel.getElement().setId(HTMLPanel.createUniqueId());
referencePanel.add(innerPanel);
}
}

public class ReferenceUI extends Composite {

interface ReferenceUIUiBinder extends
UiBinder<Widget,ReferenceUI> {
}

private static ReferenceUIUiBinder uiBinder = GWT
.create(ReferenceUIUiBinder.class);


@UiField
HTMLPanel refPanel;

public ReferenceUI() {
initWidget(uiBinder.createAndBindUi(this));
}

public CreditReferenceUI(final UiBinder<Widget, CreditReferenceUI> binder) {
initWidget(binder.createAndBindUi(this));
}

public HTMLPanel getRefPanel() {
return refPanel;
}
}

引用UI.ui.xml

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui"
xmlns:gwittir="urn:import:com.totsp.gwittir.client.ui">
<g:HTMLPanel ui:field="referencePanel">
<table>
<tr>
<td>
<b>First Name</b></td>
<td>
<b>Last Name</b></td>
<td>
<b>Phone</b></td>
<td>
<b>Fax</b></td>
</tr>
<tr>
<td>
<gwittir:TextBox ui:field="referenceFirstName" styleName="input"/></td>
<td>
<gwittir:TextBox ui:field="referenceLastName" styleName="input"/></td>
<td>
<table><tr>
<td> ( </td> <td>
<gwittir:TextBox ui:field="referencePhoneAreaCode" styleName="input" maxLength="3"/></td>
<td> ) </td> <td>
<gwittir:TextBox ui:field="referencePhoneNumber" styleName="input" maxLength="7"/></td>
<td> # </td> <td>
<gwittir:TextBox ui:field="referencePhoneExtension" styleName="input" maxLength="25"/></td>
</tr></table></td>
<td>
<table><tr>
<td> ( </td> <td>
<gwittir:TextBox ui:field="referenceFaxAreaCode" styleName="input" maxLength="3"/></td>
<td> ) </td> <td>
<gwittir:TextBox ui:field="referenceFaxNumber" styleName="input" maxLength="7"/></td>
</tr></table></td>
</tr>
<tr>
<td style="text-align:right">
<b>Address: </b> Street</td>
<td>
<gwittir:TextBox ui:field="referenceStreet" styleName="input"/></td>
<td colspan="2" style="width:50%">
<table><tr><td> City</td>
<td><gwittir:TextBox ui:field="referenceCity" styleName="input" maxLength="25"/></td>
<td> State </td>
<td class="state"><gwittir:TextBox ui:field="referenceState" styleName="input" maxLength="2"/></td>
<td> ZIP</td>
<td><gwittir:TextBox ui:field="referenceZIP" styleName="input" maxLength="9"/></td></tr></table>
</td>
</tr>
</table>
</g:HTMLPanel>
</ui:UiBinder>

AppUIDemo.ui.xml

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui"
xmlns:gwittir="urn:import:com.totsp.gwittir.client.ui">
<g:HTMLPanel ui:field="basePanel">
<!-- <div id="MainContent"> -->
<p><h2><b>Application Demo</b></h2></p>

<g:FlowPanel ui:field="referencePanel">
</g:FlowPanel>
</g:HTMLPanel>
</ui:UiBinder>

最佳答案

我将从修复开始,然后继续解释。

解决问题

解决此问题的最简单方法如下。这是您的代码:

HTMLPanel innerPanel = reference.getRefPanel();
innerPanel.getElement().setId(HTMLPanel.createUniqueId());
referencePanel.add(innerPanel);

将其替换为以下代码。请注意,只有最后一行发生了变化。

HTMLPanel innerPanel = reference.getRefPanel();
innerPanel.getElement().setId(HTMLPanel.createUniqueId());
referencePanel.add(reference);

这样,您将添加 Composite 对象。对用户来说没有区别,因为 HTMLPanel(您的 innerPanel)将直接附加到文档中。


一些背景

将小部件添加到复杂面板

当您将小部件添加到复杂面板(包含多个子小部件的面板)时,会依次发生四件事:

  1. 小部件被告知将其自身从其当前父级中移除
  2. 面板将小部件添加到其子项列表
  3. 面板将小部件添加到文档
  4. 小部件被告知将面板设置为它的新父级

告诉小部件将自己从其父级中移除

当 child 被告知将自己从其 parent 身边移除时,会发生以下情况之一:

  • 如果小部件没有父级,它什么都不做
  • 如果小部件是实现 HasWidgets 的面板的子项,则小部件会告诉面板删除该小部件
  • 否则,小部件将抛出 IllegalStateException 消息“此小部件的父级未实现 HasWidgets

复合小部件

调用 initWidget(Widget) 时,小部件的父级设置为 Composite 对象。

问题

当您尝试添加 innerPanel 时,它会被告知将其自身从其当前父级中移除。 innerPanel 实际上是 UiBinder 模板的根。它的父级是一个 Composite 对象(具体来说,ReferenceUI)。这导致异常被抛出,因为 Composite 没有实现 HasWidgets 并且不支持删除它的小部件。

关于java - 如何组合多个基于 uiBinder 的小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2822273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com