Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSNI - invoking native method from another native method

Tags:

gwt

jsni

How to call another native on button click of a native method? can we call more than 1 native method from single native method on button click


1 Answers

Yes you can do it.

Sample code:

public void onModuleLoad() {
    exportSayHello();

    Button btn = new Button("Click");
    btn.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            buttonClicked("vartika");
        }
    });

    RootPanel.get().add(btn);
}

public static native void exportSayHello() /*-{
    $wnd.sayHelloFunction = $entry(@com.gwt.test.client.GWTTestProject::sayHello(Ljava/lang/String;));
}-*/;

public static native void buttonClicked(String value)/*-{
    $wnd.sayHelloFunction(value);
}-*/;

public static native void sayHello(String value)/*-{
    $wnd.alert("Hello " + value);
}-*/;

Steps to follow:

  • Export method sayHello() to JavaScript using JSNI
  • Now call it from native method buttonClicked() using the same name sayHelloFunction that is exported to JavaScript.

Read more about GWT JSNI.

like image 104
Braj Avatar answered Aug 23 '26 09:08

Braj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!