java - Adding same widget to two panel causing issue -
i have created 2 verticalpanel(mainvp,subvp) , 1 textbox(box).textbox(box) added mainvp subvp adding mainvp rootpanel here if lauch application nothing visible though have added textbox(box) verticalpanel(mainvp) added main rootpanel.
verticalpanel mainvp=new verticalpanel(); verticalpanel subvp=new verticalpanel(); textbox box=new textbox(); mainvp.add(box); //textbox added verticalpanel subvp.add(box); rootpanel.get().add(mainvp);//mainvp contains textbox
can explain me how above code working internally?
your panel subvp
isn't added anything, , when box
added subvp
, removed mainvp
. widgets can 1 place @ time.
--
(adding more detail comment posted)
this part of basic assumption of how widget works - isn't "stamp" can put on page in several places, instead represents 1 or more dom elements, , wraps them easier use , reuse. each widget exposes event handlers can listen to, , ways change widget looks like.
imagine if had 2 buttons on page, , need them same thing. when looking @ page, there 2 buttons, , while same , cause same action, aren't same thing.
consider how button might work internally - lets has check see if mouse hovered, , if display tooltip, or change color. if same widget rendered in 2 places, both change.
from api side: widget
has method getparent()
, returns parent widget. if add widget more 1 place @ time, either getparent()
wouldn't possible, or need return list.
panel likewise has indexof
, if add widget more 1 parent, follow add same widget same parent multiple times - indexof
return?
finally, implementation achieve this. javadoc panel.add
:
/** * adds child widget. * * <p> * <b>how override method</b> * </p> * <p> * there several important things must take place in correct * order add or insert widget panel. not of these steps * relevant every panel, of steps must considered. * <ol> * <li><b>validate:</b> perform sanity checks ensure panel can * accept new widget. examples: checking valid index on insertion; * checking panel not full if there max capacity.</li> * <li><b>adjust reinsertion:</b> panels need handle case * widget child of panel. example: when performing * reinsert, index might need adjusted account widget's * removal. see {@link complexpanel#adjustindex(widget, int)}.</li> * <li><b>detach child:</b> remove widget existing parent, if * any. panels call {@link widget#removefromparent()} on * widget.</li> * <li><b>logical attach:</b> state variables of panel should * updated reflect addition of new widget. example: widget * added panel's {@link widgetcollection} @ appropriate index.</li> * <li><b>physical attach:</b> widget's element must physically * attached panel's element, either directly or indirectly.</li> * <li><b>adopt:</b> call {@link #adopt(widget)} finalize add * last step.</li> * </ol> * </p> * * @param child widget added * @throws unsupportedoperationexception if method not supported (most * means specific overload must called) * @see haswidgets#add(widget) */ public void add(widget child)
finally, default implementation used panels in gwt @ heart (complexpanel.add
):
// detach new child. child.removefromparent(); // logical attach. getchildren().add(child); // physical attach. dom.appendchild(container, child.getelement()); // adopt. adopt(child);
there other implementations well, boil down this, in keeping guidelines outlined in panel
.
Comments
Post a Comment