GWT 2.0.4 Way to query browser window's document area size in onModuleLoad

522 views
Skip to first unread message

magic

unread,
Nov 5, 2010, 5:21:54 AM11/5/10
to Google Web Toolkit
Hi,

I am looking for a way to query the size of the browser's document
window when onModuleLoad is called. I have tried

Element body = RootPanel.getBodyElement();
int clientWidth = body.getClientWidth();
int clientHeight = body.getClientHeight();

body.getClientWidth() gives the correct width, but
body.getClientHeight always returns 0. In fact all queries on body
having to do with height return 0.

I had similar problems in pure javascript with

dw = document.body.offsetWidth;
dh = document.body.offsetHeight;

I overcame it there with the following:

// IE does not appear to support window.innerHeight.
if (typeof(window.innerHeight) == 'number')
dh = window.innerHeight;
else if (document.documentElement &&
document.documentElement.clientHeight)
dh = document.documentElement.clientHeight;
else if (document.body & document.body.clientHeight)
dh = document.body.clientHeight;

Anyway to do something similar via GWT?

-Mark

Craig

unread,
Nov 5, 2010, 5:45:20 PM11/5/10
to Google Web Toolkit
Have you tried Window.getClientHeight()?

magic

unread,
Nov 9, 2010, 3:48:08 AM11/9/10
to Google Web Toolkit
I have now thank you. Yes it gives me the height of the window.

What I want to do is size a canvas so it fills the window. However the
canvas is inside the document body so has the body's borders around
it. That is why I am using body.getClientWidth() which gives the width
inside the borders. To use Window.getClientHeight, I need to subtract
the border height before using the value to size my canvas. I can't
find any way to get the border. body.get{Absolute,Offset}{Left,Top}
all return 0 as does body.getOffsetHeight. body.getOffsetWidth returns
the same value as body.getClientWidth. (In all cases body =
RootPanel.getBodyElement()).

Any suggestions for getting the border height?

-Mark

cyclingthealps

unread,
Nov 29, 2011, 3:47:26 AM11/29/11
to google-we...@googlegroups.com
Hi,
The post is a little old but I would just like to share a solution.
In my onModuleLoad() method of the "main" class in my GWT project the first thing I do is setting the height of the rootPanel to 100%.

RootPanel.get().setHeight("100%");

From here I can then work with the rootPanel object to obtain the offSetHeight which can then be used to set the height of my other panels.

I have a menuBar which is a horizontalPanel and the height is 60px. Below the menu bar I have a flowPanel and I set the height by obtaining the rootPanel height - 60px. I fill this flowPanel with a Map where the Width and Height is set to100%.

FlowPanel mapPanel = new FlowPanel();
int height = RootPanel.get().getOffsetHeight() - 60;
mapPanel.setHeight((new Integer(height)).toString());
mapPanel.add(Map.getMap());

Hope this helps.

Cheers,

Dennis

Reply all
Reply to author
Forward
0 new messages