0

I want to call gwt method from html tag . I did

 public void onModuleLoad(){
         HTML html = new HTML("<button onclick=\"javascript:fire();\">test</button>");
         RootPanel.get().add(html);
    }

    private static  native void  fire()/*-{
      $wnd.alert("clicked");
    }-*/;

but this code does not work . Can somebody help me?

3
  • This won't work. I am not sure but I think will remove the fire method, because it is not used in the GWT side. Have you tried to use a TextButton instead? Commented Feb 11, 2016 at 10:46
  • but fire is native javascript method .. can you please give me some working example Commented Feb 11, 2016 at 11:02
  • I assume the alert does not pop up, and that's what's not working? Commented Feb 15, 2016 at 3:43

2 Answers 2

2

Try:

Button tb = new Button("test");
tb.addClickHandler(new ClickHandler() {
  @Override
  public void onClick(ClickEvent event) {
    fire();
  }
});
RootPanel.get().add(tb);

private void fire() {
  com.google.gwt.user.client.Window.alert("clicked");
}

Something like this should work. (There are maybe some typos.)

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

2 Comments

Which part is essential to not change? The Java side is compiled (and wrapped up in an iframe to prevent namespace issues), and so the JS side can't see it (and wouldn't be nicely compiled, removing the entire point of GWT). The "way [you] have mentioned" is why it doesn't work, so something has to change.
You can use the original native JSNI implementation of fire() in place of the one in this Answer
1

GWT has JSNI and JSInterop. both can expose java api to js. this excerpt is taken from official gwt documentation.

Calling a Java Method from Handwritten JavaScript

Sometimes you need to access a method or constructor defined in GWT from outside JavaScript code. This code might be hand-written and included in another java script file, or it could be a part of a third party library. In this case, the GWT compiler will not get a chance to build an interface between your JavaScript code and the GWT generated JavaScript directly.

A way to make this kind of relationship work is to assign the method via JSNI to an external, globally visible JavaScript name that can be referenced by your hand-crafted JavaScript code. package mypackage;

public MyUtilityClass
{
    public static int computeLoanInterest(int amt, float interestRate,
                                          int term) { ... }
    public static native void exportStaticMethod() /*-{
       $wnd.computeLoanInterest =
          $entry(@mypackage.MyUtilityClass::computeLoanInterest(IFI));
    }-*/;
}

Notice that the reference to the exported method has been wrapped in a call to the $entry function. This implicitly-defined function ensures that the Java-derived method is executed with the uncaught exception handler installed and pumps a number of other utility services. The $entry function is reentrant-safe and should be used anywhere that GWT-derived JavaScript may be called into from a non-GWT context.

On application initialization, call MyUtilityClass.exportStaticMethod() (e.g. from your GWT Entry Point). This will assign the function to a variable in the window object called computeLoanInterest.

here's a link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.