Label + Hyperlink + Label

1,270 views
Skip to first unread message

Brian

unread,
Oct 3, 2008, 10:06:05 AM10/3/08
to Google Web Toolkit
I'm wondering if this is really the best approach, of it I'm one
widget short of a better idea.

I'm just trying to write some text, a gwt Hyperlink, and more text.
Basically trying to concatenate labels and Hyperlinks.

I can do this by creating a HorizontalPanel, adding the label, then
adding the Hyperlink, then adding more labels, more Hyperlinks, and so
on. But is it really necessary for the HorizontalPanel to be there?
Is there something else that would be better? Just off hand, I
thought of things like taking the Hyperlink's .getHTML() and
concatenating that with the label's text, but that isn't what I'm
after.

Thanks

Ian Bambury

unread,
Oct 3, 2008, 10:37:31 AM10/3/08
to Google-We...@googlegroups.com
You could try
 
HTMLPanel p = new HTMLPanel("Click <span id='link'></span> to go there");
p.add(new Hyperlink("here", "token"), "link");
 
(I might have the parameters the wrong way around, but you get the idea.)
2008/10/3 Brian <hib...@gmail.com>

Brian

unread,
Oct 3, 2008, 11:24:16 AM10/3/08
to Google Web Toolkit
Tricky, but doesn't quite work in my attempts.

Both HTMLPanel.add() and HTMLPanel.addAndReplaceElement() puts a
newline before the link, so I end up with:

blah blah
link
blah blah

instead of: blah blah link blah blah

I'm putting the HTMLPanel in a FlexTable's cell.


On Oct 3, 10:37 am, "Ian Bambury" <ianbamb...@gmail.com> wrote:
> You could try
>
> HTMLPanel p = new HTMLPanel("Click <span id='link'></span> to go there");
> p.add(new Hyperlink("here", "token"), "link");
>
> (I might have the parameters the wrong way around, but you get the idea.)
>
> Ian
>
> http://examples.roughian.com
>
> 2008/10/3 Brian <hibr...@gmail.com>

Thomas Broyer

unread,
Oct 3, 2008, 11:44:55 AM10/3/08
to Google Web Toolkit


On 3 oct, 17:24, Brian <hibr...@gmail.com> wrote:
> Tricky, but doesn't quite work in my attempts.
>
> Both HTMLPanel.add() and HTMLPanel.addAndReplaceElement() puts a
> newline before the link, so I end up with:
>
> blah blah
> link
> blah blah
>
> instead of: blah blah link blah blah
>
> I'm putting the HTMLPanel in a FlexTable's cell.

It has nothing to do with the HTMLPanel actually, but a problem with
the Hyperlink widget: it's made up of a DIV wrapping an A, that's this
DIV wrapper that's causing the line breaks, because a DIV is
display:block by default.

I'd suggest replacing the Hyperlink with an Anchor+ClickListener.
I'd also use a FlowPanel+InlineLabels but it's up to you, the
HTMLPanel should work like a charm too:

FlowPanel container = new FlowPanel();
container.add(new InlineLabel("some text ");
Anchor link = new Anchor("link", "#token");
link.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
History.newItem("token");
DOM.eventGetCurrentEvent().preventDefault();
}
});
container.add(link);
container.add(new InlineLabel(" some more text");

...and/or you could "vote for" (i.e. "star") the issue 2901:
http://code.google.com/p/google-web-toolkit/issues/detail?id=2901

Brian

unread,
Oct 3, 2008, 11:59:36 AM10/3/08
to Google Web Toolkit
If it weren't such pita, it'd be sweet. But yeah, that worked great.

Since I'm doing this on a number of links in a bunch of text, I
wrapped up the click listener as such instead of new'ing one each
time.

ClickListener linkClickListener = new ClickListener() {
public void onClick(Widget sender) {
History.newItem(((Anchor)sender).getHref().substring(1));
DOM.eventGetCurrentEvent().preventDefault();
}
};

Thanks much.

Brian

unread,
Oct 3, 2008, 12:10:55 PM10/3/08
to Google Web Toolkit
... and incidently, the horizontalpanel approach never did work well.
Before I could figure out the problem, I just did the flowpanel+anchor
+clicklistener approach. Who woulda thought inline links would be
such a pain.

I star'd that issue...

Ian Bambury

unread,
Oct 3, 2008, 1:46:37 PM10/3/08
to Google-We...@googlegroups.com
Why faff about with an anchor and clicklistener when you can just put this
 
.gwt-Hyperlink
{
    display                     :   inline;
}
 
in your css?
 
You can then throw one lot of text at the HTMLPanel with <span> placeholders.

Brian

unread,
Oct 13, 2008, 3:53:37 PM10/13/08
to Google Web Toolkit
I had to revisit the code with the labels and links together, and
decided not to 'faff about' anymore. I added a style to my links, and
did your css below. Worked great, and a lot less code! Thanks much,
I hadn't thought of solving it with css for some reason.

Charles Fry

unread,
Oct 29, 2008, 6:00:38 PM10/29/08
to Google Web Toolkit
Yikes. That seems like a defect in Hyperlink. Why would it insist on
being a div?

On Oct 3, 1:46 pm, "Ian Bambury" <ianbamb...@gmail.com> wrote:

sal...@gmail.com

unread,
Oct 30, 2008, 10:05:29 AM10/30/08
to Google Web Toolkit
I can't think of a widget, besides a Table, that isn't a div. This
InlineLabel class is new to me though. I have an extension of
Hyperlink called SimpleHyperlink that adds display:inline to its
style. Same goes for SimpleLabel.
Reply all
Reply to author
Forward
0 new messages