Inserting Widgets and simple text into panel

14 views
Skip to first unread message

Ice13ill

unread,
Nov 3, 2011, 9:30:27 AM11/3/11
to Google Web Toolkit
Hello, I want create a panel with a flow layout (for ex. FlowPanel)
which contains 2 widgets and a large text at the end (not a <div> or
another element)
I tried to add the widgets and then set the panel's element text:

flowPanel.add(w1)
flowPanel.add(w2)
flowPanel.getElement.setInnerText(largeText)

but the last line overrides all other widgets.

How can i do this (but without using UIBinder for now.) ?

Alan Chaney

unread,
Nov 3, 2011, 11:20:41 AM11/3/11
to google-we...@googlegroups.com
Ice13ill

On 11/3/2011 6:30 AM, Ice13ill wrote:
> Hello, I want create a panel with a flow layout (for ex. FlowPanel)
> which contains 2 widgets and a large text at the end (not a<div> or
> another element)
> I tried to add the widgets and then set the panel's element text:
>
> flowPanel.add(w1)
> flowPanel.add(w2)
> flowPanel.getElement.setInnerText(largeText)

A FlowPanel is a container for widgets - not a container for widgets
and some text at end!.

Its a bit of a simplification, but basically gwt widgets render by doing
exactly what you've done in the 3rd line. The best you could do is to
retrieve the inner text and append your text to it - but really, that's
a hack.

My preference would be to put the text in an appropriate widget, such
as an HTML Widget and add that instead of manipulating the text
directly, or if you want the text to be unstyled, then use a Label widget.

HTH

Alan

Andrei Cosmin Fifiiţă

unread,
Nov 3, 2011, 11:26:34 AM11/3/11
to google-we...@googlegroups.com
Thank you for the reply Alan, but the reason for trying this approach is because widgets have much more that simple html elements (for example click handlers for click events) if i set the inner html or text of a panel (or a HTML widget) the new elements will be rendered ok, but the listeners/handlers (both native and custom) will be lost

--
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-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsub...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


Steve Moyer

unread,
Nov 3, 2011, 9:05:33 PM11/3/11
to Google Web Toolkit
Try:

flowPanel.add(w1);
flowPanel.add(w2);
flowPanel.getElement.setInnerText(flowPanel.getInnerHTML() +
largeText);

This might be dangerous in some instances, but the widgets should be
well-formed HTML, so adding your text to the end should work.

Good luck!

Thomas Broyer

unread,
Nov 3, 2011, 11:06:21 PM11/3/11
to google-we...@googlegroups.com
As Ice13ill said, setting innerText or innerHTML will break the w1 and w2 widgets (their element are rebuilt, so the instance they reference are no longer in the document, and event handlers obviously fails too (no longer registered, to begin with)).

If using a Label or HTML (or InlineLabel/InlineHTML) is not an option:
   String w1Id = HTMLPanel.createUniqueId();
   String w2Id = HTMLPanel.createUniqueId();
   HTMLPanel panel = new HTMLPanel("<span id='" + w1Id + "'></span><span id='" + w1Id + "'></span>large text");
   panel.addAndReplaceElement(w1, w1Id);
   panel.addAndReplaceElement(w2, w2Id);

Alternatively, something can be hacked that way:
   flowPanel.add(w1);
   flowPanel.add(w2);
   flowPanel.getElement().appendChild(Document.get().createTextNode(...));
Use at your own risk.

Andrei Cosmin Fifiiţă

unread,
Nov 7, 2011, 6:12:50 AM11/7/11
to google-we...@googlegroups.com
I used the last solution and it worked fine (even IE 7,8 :), but I haven't tested IE 6 )

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/NftmeExljfkJ.

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