将相同的小部件添加到导致问题的两个面板
Adding same widget to two panel causing issue
我创建了两个 VerticalPanel(mainVP,subVP) 和一个 TextBox(box)。TextBox(box) 被添加到 mainVP 和 subVP,但我在这里只将 mainVP 添加到 RootPanel 如果我启动我的应用程序,则什么都没有即使我已将 TextBox(box) 添加到添加到主 Rootpanel 的 VerticalPanel(mainVP) 也是可见的。
VerticalPanel mainVP=new VerticalPanel();
VerticalPanel subVP=new VerticalPanel();
TextBox box=new TextBox();
mainVP.add(box); //Textbox added to VerticalPanel
subVP.add(box);
RootPanel.get().add(mainVP);//mainVP contains TextBox
谁能解释一下上面的代码在内部是如何工作的?
您的面板 subVP
未添加到任何内容,当 box
添加到 subVP
时,它会从 mainVP
中删除。小部件一次只能在一个地方。
--
(为发表的评论添加更多细节)
这是小部件如何工作的基本假设的一部分 - 它不是可以放在页面上多个位置的 "stamp",而是代表一个或多个 DOM元素,并将它们包装起来以便于使用和重用。每个小部件都会公开一些您可以收听的事件处理程序,以及更改小部件外观的方法。
想象一下,如果您在页面上有两个按钮,并且您需要它们执行相同的操作。查看该页面时,很明显有两个按钮,虽然它们看起来相同并导致相同的操作,但它们并不是同一回事。
考虑按钮在内部的工作方式 - 假设它检查鼠标是否悬停,如果悬停则显示工具提示或更改颜色。如果同一个小部件在两个地方呈现,现在两个地方都会得到这个变化。
从 API 方面:Widget
有一个方法 getParent()
,它 return 是父窗口小部件。如果您一次可以将小部件添加到多个位置,要么 getParent()
不可能,要么需要 return 一个列表。
面板同样有 indexOf
,但如果您可以将一个小部件添加到多个父级,那么您也可以将同一个小部件多次添加到同一父级 - indexOf
return?
终于实现了。来自 Panel.add
:
的 Javadoc
/**
* Adds a child widget.
*
* <p>
* <b>How to Override this Method</b>
* </p>
* <p>
* There are several important things that must take place in the correct
* order to properly add or insert a Widget to a Panel. Not all of these steps
* will be relevant to every Panel, but all of the steps must be considered.
* <ol>
* <li><b>Validate:</b> Perform any sanity checks to ensure the Panel can
* accept a new Widget. Examples: checking for a valid index on insertion;
* checking that the Panel is not full if there is a max capacity.</li>
* <li><b>Adjust for Reinsertion:</b> Some Panels need to handle the case
* where the Widget is already a child of this Panel. Example: when performing
* a reinsert, the index might need to be adjusted to account for the Widget's
* removal. See {@link ComplexPanel#adjustIndex(Widget, int)}.</li>
* <li><b>Detach Child:</b> Remove the Widget from its existing parent, if
* any. Most Panels will simply call {@link Widget#removeFromParent()} on the
* Widget.</li>
* <li><b>Logical Attach:</b> Any state variables of the Panel should be
* updated to reflect the addition of the new Widget. Example: the Widget is
* added to the Panel's {@link WidgetCollection} at the appropriate index.</li>
* <li><b>Physical Attach:</b> The Widget's Element must be physically
* attached to the Panel's Element, either directly or indirectly.</li>
* <li><b>Adopt:</b> Call {@link #adopt(Widget)} to finalize the add as the
* very last step.</li>
* </ol>
* </p>
*
* @param child the widget to be added
* @throws UnsupportedOperationException if this method is not supported (most
* often this means that a specific overload must be called)
* @see HasWidgets#add(Widget)
*/
public void add(Widget child)
最后,GWT 中大多数面板使用的默认实现基本上是这样的 (ComplexPanel.add
):
// Detach new child.
child.removeFromParent();
// Logical attach.
getChildren().add(child);
// Physical attach.
DOM.appendChild(container, child.getElement());
// Adopt.
adopt(child);
还有其他实现,但它们主要归结为这一点,与 Panel
中概述的准则保持一致。
我创建了两个 VerticalPanel(mainVP,subVP) 和一个 TextBox(box)。TextBox(box) 被添加到 mainVP 和 subVP,但我在这里只将 mainVP 添加到 RootPanel 如果我启动我的应用程序,则什么都没有即使我已将 TextBox(box) 添加到添加到主 Rootpanel 的 VerticalPanel(mainVP) 也是可见的。
VerticalPanel mainVP=new VerticalPanel();
VerticalPanel subVP=new VerticalPanel();
TextBox box=new TextBox();
mainVP.add(box); //Textbox added to VerticalPanel
subVP.add(box);
RootPanel.get().add(mainVP);//mainVP contains TextBox
谁能解释一下上面的代码在内部是如何工作的?
您的面板 subVP
未添加到任何内容,当 box
添加到 subVP
时,它会从 mainVP
中删除。小部件一次只能在一个地方。
--
(为发表的评论添加更多细节)
这是小部件如何工作的基本假设的一部分 - 它不是可以放在页面上多个位置的 "stamp",而是代表一个或多个 DOM元素,并将它们包装起来以便于使用和重用。每个小部件都会公开一些您可以收听的事件处理程序,以及更改小部件外观的方法。
想象一下,如果您在页面上有两个按钮,并且您需要它们执行相同的操作。查看该页面时,很明显有两个按钮,虽然它们看起来相同并导致相同的操作,但它们并不是同一回事。
考虑按钮在内部的工作方式 - 假设它检查鼠标是否悬停,如果悬停则显示工具提示或更改颜色。如果同一个小部件在两个地方呈现,现在两个地方都会得到这个变化。
从 API 方面:Widget
有一个方法 getParent()
,它 return 是父窗口小部件。如果您一次可以将小部件添加到多个位置,要么 getParent()
不可能,要么需要 return 一个列表。
面板同样有 indexOf
,但如果您可以将一个小部件添加到多个父级,那么您也可以将同一个小部件多次添加到同一父级 - indexOf
return?
终于实现了。来自 Panel.add
:
/**
* Adds a child widget.
*
* <p>
* <b>How to Override this Method</b>
* </p>
* <p>
* There are several important things that must take place in the correct
* order to properly add or insert a Widget to a Panel. Not all of these steps
* will be relevant to every Panel, but all of the steps must be considered.
* <ol>
* <li><b>Validate:</b> Perform any sanity checks to ensure the Panel can
* accept a new Widget. Examples: checking for a valid index on insertion;
* checking that the Panel is not full if there is a max capacity.</li>
* <li><b>Adjust for Reinsertion:</b> Some Panels need to handle the case
* where the Widget is already a child of this Panel. Example: when performing
* a reinsert, the index might need to be adjusted to account for the Widget's
* removal. See {@link ComplexPanel#adjustIndex(Widget, int)}.</li>
* <li><b>Detach Child:</b> Remove the Widget from its existing parent, if
* any. Most Panels will simply call {@link Widget#removeFromParent()} on the
* Widget.</li>
* <li><b>Logical Attach:</b> Any state variables of the Panel should be
* updated to reflect the addition of the new Widget. Example: the Widget is
* added to the Panel's {@link WidgetCollection} at the appropriate index.</li>
* <li><b>Physical Attach:</b> The Widget's Element must be physically
* attached to the Panel's Element, either directly or indirectly.</li>
* <li><b>Adopt:</b> Call {@link #adopt(Widget)} to finalize the add as the
* very last step.</li>
* </ol>
* </p>
*
* @param child the widget to be added
* @throws UnsupportedOperationException if this method is not supported (most
* often this means that a specific overload must be called)
* @see HasWidgets#add(Widget)
*/
public void add(Widget child)
最后,GWT 中大多数面板使用的默认实现基本上是这样的 (ComplexPanel.add
):
// Detach new child.
child.removeFromParent();
// Logical attach.
getChildren().add(child);
// Physical attach.
DOM.appendChild(container, child.getElement());
// Adopt.
adopt(child);
还有其他实现,但它们主要归结为这一点,与 Panel
中概述的准则保持一致。