Is it possible to _replace_ element with my widget?

268 views
Skip to first unread message

Jaroslav Záruba

unread,
Jun 18, 2010, 9:55:56 PM6/18/10
to Google Web Toolkit
Hi

I know how to insert widget into element...
RootPanel.get("placeholder").add(new MyWidget("hello"));

But is it possible to replace element with my widget?

I already tried following...
Element placeholder = RootPanel.get("debatePlaceholder").getElement();
placeholder.getParentElement().replaceChild(commentPanel.getElement(), placeholder);

But the widget seems to be dead, i.e. it does not receive ClickEvents. :(

Regards
  J. Záruba

Stefan Bachert

unread,
Jun 19, 2010, 9:59:26 AM6/19/10
to Google Web Toolkit
Hi Jaroslav,

I am quite sure that it would be possible somehow, but dont I see any
need!?
Just omit "debatePlaceHolder" and put an id to its parent and add your
widget to that parent. No need to replace.

What do want to achieve with your approach?


Stefan Bachert
http://gwtworld.de

On Jun 19, 3:55 am, Jaroslav Záruba <jaroslav.zar...@gmail.com> wrote:
> Hi
>
> I know how to insert widget into element...
> RootPanel.get("placeholder").add(new MyWidget("hello"));
>
> But is it possible to *replace* element with my widget?

Jaroslav Záruba

unread,
Jun 19, 2010, 11:04:57 AM6/19/10
to google-we...@googlegroups.com
On Sat, Jun 19, 2010 at 3:59 PM, Stefan Bachert <stefan...@yahoo.de> wrote:
Hi Jaroslav,

I am quite sure that it would be possible somehow

How pwetty please? :(
 
Stefan Bachert
http://gwtworld.de

On Jun 19, 3:55 am, Jaroslav Záruba <jaroslav.zar...@gmail.com> wrote:
> Hi
>
> I know how to insert widget into element...
> RootPanel.get("placeholder").add(new MyWidget("hello"));
>
> But is it possible to *replace* element with my widget?
>
> I already tried following...
> Element placeholder = RootPanel.get("debatePlaceholder").getElement();
> placeholder.getParentElement().replaceChild(commentPanel.getElement(),
> placeholder);
>
> But the widget seems to be dead, i.e. it does not receive ClickEvents. :(
>
> Regards
>   J. Záruba

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


André Moraes

unread,
Jun 19, 2010, 11:44:27 AM6/19/10
to Google Web Toolkit
Jaroslav,

Maybe instead of reaplacing you delete than insert again the element?!

I think that the problem of not getting Clicks is because of the way
GWT handles clicks.

To avoid memory leaks, the handlers don't go in the same way that was
generally done without GWT.

Another approach will be make that placehoder element be an actual GWT
Widget (FlowPanel is simply a DIV) and then you add your widgets to
that panel.

On 19 jun, 12:04, Jaroslav Záruba <jaroslav.zar...@gmail.com> wrote:
> On Sat, Jun 19, 2010 at 3:59 PM, Stefan Bachert <stefanbach...@yahoo.de>wrote:
>
> > Hi Jaroslav,
>
> > I am quite sure that it would be possible somehow
>
> How pwetty please? :(
>
>
>
> > Stefan Bachert
> >http://gwtworld.de
>
> > On Jun 19, 3:55 am, Jaroslav Záruba <jaroslav.zar...@gmail.com> wrote:
> > > Hi
>
> > > I know how to insert widget into element...
> > > RootPanel.get("placeholder").add(new MyWidget("hello"));
>
> > > But is it possible to *replace* element with my widget?
>
> > > I already tried following...
> > > Element placeholder = RootPanel.get("debatePlaceholder").getElement();
> > > placeholder.getParentElement().replaceChild(commentPanel.getElement(),
> > > placeholder);
>
> > > But the widget seems to be dead, i.e. it does not receive ClickEvents. :(
>
> > > Regards
> > >   J. Záruba
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > To post to this group, send email to google-we...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsubs cr...@googlegroups.com>
> > .

Jaroslav Záruba

unread,
Jun 19, 2010, 12:03:02 PM6/19/10
to google-we...@googlegroups.com
2010/6/19 André Moraes <and...@gmail.com>

Jaroslav,

Maybe instead of reaplacing you delete than insert again the element?!

I don't understand. What would be the benefit? :)
Let's say the placeholder element was third node within its parent. How can I place my widget in its place?
(I do apologize, I'm still new to GWT.)
 
I think that the problem of not getting Clicks is because of the way
GWT handles clicks.

I believe I would have to call Panel.adopt(widget), judging by GWT-sources... Unfortunately RootPanel does not expose adopt. :(
 
Another approach will be make that placehoder element be an actual GWT
Widget (FlowPanel is simply a DIV) and then you add your widgets to
that panel.

I only know id of the placeholder element, I'm not creating it, therefore I can't make it a Widget.
After a widget is created it should replace that element and the element should go to eternity.

Jaroslav Záruba

unread,
Jun 19, 2010, 12:27:32 PM6/19/10
to google-we...@googlegroups.com
I'm creating widget that is supposed to check whether there's an <a/> element with known id in the page. If the element is found my widget reads the href attribute and loads stuff from that URL, then displays the (processed) content exactly where user indicated by that <a/>. (Placing my widget into <a/> has several obvious side-effect I don't like.)
If the element is not found then the link works as usually.

Is the fact that this is impossible to do a mere result of 'no one thought anyone would need that'? :)

2010/6/19 Jaroslav Záruba <jarosla...@gmail.com>

André Moraes

unread,
Jun 19, 2010, 2:55:51 PM6/19/10
to Google Web Toolkit
Jaroslav, this code does more or less what you want.

package br.com.andre.gwt.samples.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class Samples implements EntryPoint {
@Override
public void onModuleLoad() {
MyButton button = new MyButton("<h1> this is a nice html</h1>");
Element htmlEl = button.getElement();

button.addClickHandler(new ClickHandler() {

@Override
public void onClick(ClickEvent event) {
Window.alert("Houston We have lift-off!");
}
});

RootPanel myRoot = RootPanel.get("placeholder");
if (myRoot != null) {
Element myRootParent = myRoot.getElement().getParentElement();
if (myRootParent != null) {
myRootParent.insertAfter(htmlEl, myRoot.getElement());
myRootParent.removeChild(myRoot.getElement());
}
}

button.attach();

}

public class MyButton extends Button {

public MyButton(String html) {
super(html);
}

public void attach() {
this.onAttach();

RootPanel.detachOnWindowClose(this);
}
}
}

I used the Idea which was implemented in the method wrap of the Button
class

Button.wrap(element);

As far as i read the docs, the onAttach() allows the element to start
receiving events from the browser.

Hope it helps,


On 19 jun, 13:27, Jaroslav Záruba <jaroslav.zar...@gmail.com> wrote:
> I'm creating widget that is supposed to check whether there's an <a/>
> element with known id in the page. If the element is found my widget reads
> the href attribute and loads stuff from that URL, then displays
> the (processed) content exactly where user indicated by that <a/>. (Placing
> my widget into <a/> has several obvious side-effect I don't like.)
> If the element is not found then the link works as usually.
>
> Is the fact that this is impossible to do a mere result of 'no one thought
> anyone would need that'? :)
>
> 2010/6/19 Jaroslav Záruba <jaroslav.zar...@gmail.com>
>
>
>
> > 2010/6/19 André Moraes <andr...@gmail.com>

Jaroslav Záruba

unread,
Jun 19, 2010, 3:24:11 PM6/19/10
to google-we...@googlegroups.com
Thanks a lot, André.
I wasn't 'brave' enough to inspect what Panel.adopt(Widget) does, but if calling Widget.onAttach() is the trick then it isn't that bad after all. :P

Thank you very much!

Best regards
  J. Záruba

2010/6/19 André Moraes <and...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages