How to determine pixel size of Label widget at run time?

471 views
Skip to first unread message

Mike Jacobs

unread,
Aug 16, 2009, 2:10:51 PM8/16/09
to Google Web Toolkit
I have a case where I am dynamically adding a Label widget with
program determined label text. (It is adding a Label on top of a
Google Map but this question is about GWT widgets). Since the label
text can vary at run time and I would like to center the label at a
specific pixel location, I need to figure out the pixel size of the
label to position it properly.

I can specify a pixel width and height when creating the Label. Is it
possible to determine the appropriate size of the Label based on the
label text length? Is is possible to ask the label for it's pixel
size afterward instead?

I have been using a simple approach of using width = text length * 11
(since the font is 11px) but that appears to be off by a few pixels
presumably due to varying width of characters.

Thanks,
Mike

Ian Bambury

unread,
Aug 16, 2009, 6:19:11 PM8/16/09
to Google-We...@googlegroups.com
Just add it, get the width and then place it where you want it.

Mike Jacobs

unread,
Aug 16, 2009, 6:40:14 PM8/16/09
to Google Web Toolkit
There is no method to get the width on the Label widget or any of the
super classes. How would I get the width?

On Aug 16, 5:19 pm, Ian Bambury <ianbamb...@gmail.com> wrote:
> Just add it, get the width and then place it where you want it.
> Ian
>
> http://examples.roughian.com
>
> 2009/8/16 Mike Jacobs <mike.jac...@indietechnologies.com>

Ian Bambury

unread,
Aug 16, 2009, 8:15:10 PM8/16/09
to Google-We...@googlegroups.com

        Label label = new Label("Some Text)");
        RootPanel.get().add(label);
        int length = label.getOffsetWidth();


Ian

http://examples.roughian.com


2009/8/16 Mike Jacobs <mike....@indietechnologies.com>

Mike Jacobs

unread,
Aug 17, 2009, 8:27:31 AM8/17/09
to Google Web Toolkit
The label offset width is zero prior to adding to the root panel and
unusually large after adding it. The width is supposedly 761 for a
string of "~~testing~~". I will experiment with this a bit more but
this width seems like it might be the width of the root panel at the
time.

On Aug 16, 7:15 pm, Ian Bambury <ianbamb...@gmail.com> wrote:
>         Label label = new Label("Some Text)");
>         RootPanel.get().add(label);
>         int length = label.getOffsetWidth();
>
> Ian
>
> http://examples.roughian.com
>
> 2009/8/16 Mike Jacobs <mike.jac...@indietechnologies.com>

Mike Jacobs

unread,
Aug 17, 2009, 8:40:58 AM8/17/09
to Google Web Toolkit
Once I added the label to the intended container (the map pane in the
Google map API), the width was calculated correctly. Since I cannot
move the widget once it has been added, I add it to get the dimensions
and then remove it before adding it where it belongs.

Thanks for the help.

Mike

On Aug 17, 7:27 am, Mike Jacobs <mike.jac...@indietechnologies.com>
wrote:

Ian Bambury

unread,
Aug 17, 2009, 8:56:26 AM8/17/09
to Google-We...@googlegroups.com
Unless you set the label to display:block it will expand to the width of its container (it is actually a div). With display:block it will just be as wide as it needs.

Why can't you move it once it is added?
Reply all
Reply to author
Forward
0 new messages