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");

    }
}
1
  • Please share the code in your question that isn't working correctly. See stackoverflow.com/help/on-topic for more details. Commented Dec 15, 2014 at 14:48

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

my problem is different... I have to launch a Javascript Function, only after all object are loaded in page
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.

4 Comments

Yes but I've a lot of object to add to the page and I've to launch the function only when all object are loaded...
What exactly do you mean by "objects"? and "loaded"?
I read data from a db and for each record I create a Div. I want to lauch a Javascript function after all DIV are in page.
Can't you just be explicit and call a method or fire an event after you processed all records?
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.

2 Comments

Excuse me, I don't understand where you tell the Scheduler to wait until render complete?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.