1

I've to launch a Javascript function from Java only when all div are loaded into my VerticalPanel, otherwise my Javascript function will throw an exception because of the element is not yet in the page. How can I do?

This is my code:

@UiField VerticalPanel areaBody;

areaBody.addHandler(new MyLoadHandler(), LoadEvent.getType());


public class MyLoadHandler implements LoadHandler {

    @Override
    public void onLoad(LoadEvent event) {
        // TODO Auto-generated method stub

        Window.alert("onLoad");

    }
}
Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
Edo106
  • 13
  • 4
  • Please share the code in your question that isn't working correctly. See http://stackoverflow.com/help/on-topic for more details. – Chrismas007 Dec 15 '14 at 14:48

3 Answers3

2

to launch javascript from java you can use native methods:

public static native void alert(String msg) /*-{
  $wnd.alert(msg);
}-*/;

for a short example look here

more explanation you can find in this documentation

EDIT: if I got it right then just add a Scheduler which will execute when control is returned to the JavaScript event loop.

Scheduler.get().scheduleDeferred(new ScheduledCommand() {
    @Override
    public void execute() {
        executeYourJavaScript(); 
    }
});

more explanation here and a little example

Community
  • 1
  • 1
Tobika
  • 1,037
  • 1
  • 9
  • 18
  • my problem is different... I have to launch a Javascript Function, only after all object are loaded in page – Edo106 Dec 15 '14 at 16:00
0

GWT has an AttachEvent for that. Just addAttachHandler on any widget to be notified when it's added to (or removed from) the document. If you want a one-shot event, then just remove your handler when it's been called.

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
0

Use Scheduler when you need a browser to complete whatever it is currently doing before you tell it to do something else. For example, if you want to do something with a dialog only after it fully renders:

myDialogBox.show();
Scheduler.get().scheduleDeferred(new ScheduledCommand() {

    @Override
    public void execute() {
        myTextBox.setFocus();
    }
});

In this example, focus will not be set until the browser completes rendering of the dialog, so you tell the program to wait until the browser is ready.

UPDATE:

Based on your comments to other answers, you probably face a different problem: waiting for all the data to load from the server. This is done using asynchronous callback methods.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • Excuse me, I don't understand where you tell the Scheduler to wait until render complete? – Edo106 Dec 15 '14 at 18:09
  • `scheduleDeferred` command tells the browser to wait until everything renders. You should call it after you attach your panel. I also updated my answer with another suggestion. – Andrei Volgin Dec 15 '14 at 18:15