RSS
 

GWT- How to reference the body in your java code

10 juin

I was confronted with this problem. How to retrieve Element references the body. As GWT API doesn’t provide this service directly you need to use an other way.

You can add for example an id to the body in the HTML description of the page
<html> <head> <meta name='gwt:module' content='fr.improve.testGWT.gwt.Application'> </head> <body id='bodyId'> <script language="javascript" src="gwt.js"></script> </body> </html>

In your java application you just have to use the following code:

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

Element body= DOM.getElementById(« bodyId »);

Another solution is to use the JSNI with the following method:

public static native Element getBody() /*-{ return $doc.body; }-*/;

The body element is retrieve with this call:

Element body = getBody();

And you don’t need to change the HTML page of your application.
It is a little bit strange that base element like the body can’t be directly accessible

 
4 Comments

Posted by Cyril Balit in Javascript, RIA-RDA-RWA

 
  • http://rialto.application-servers.com/wiki/ Cyril Balit

    In fact the method exist in the GWT Api

    import com.google.gwt.user.client.ui.RootPanel;
    Element body = RootPanel.getBodyElement();

  • Paltonio Fraga

    Je commence au GWT, comme je n’ai pas eu accés à la documentation, je trouve très outile des interventions comme celle-ci. Merci

  • http://www.catalyst-energy.com Natural energy

    Nice blog.I like the content and think that this blog is full of rich contents.I also think that there is a need to improve the natural energy
    resources so that we can perform better.
    Thanks for this blog.I’ll refer it to my friends and will bookmark it

  • Foo

    Nowadays you can do Document.get().getBody().