Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Created February 22, 2019 18:17
Show Gist options
  • Save branflake2267/6c26ad44f4f7ec37a9bc2f7e7eb4c8df to your computer and use it in GitHub Desktop.
Save branflake2267/6c26ad44f4f7ec37a9bc2f7e7eb4c8df to your computer and use it in GitHub Desktop.
GXT 4.x JSNI example. Start the app later, by importing the script. Call into the application from Javascript.
package com.sencha.gxt.test.client.jsni_callinto;
import com.google.gwt.core.client.EntryPoint;
import com.sencha.gxt.widget.core.client.info.Info;
public class JsniExample implements EntryPoint {
private native void myInfo() /*-{
function myInfo(value) {
// Call static java method
// TODO: change the package path based on where you put this file...
$entry(@com.sencha.gxt.test.client.jsni_callinto.JsniExample::myJavaInfo(Ljava/lang/String;)(value));
}
// Add the method to the window, so it can be called from anywhere
// Goto web browser console and call > this.myInfo('Test');
$wnd.myInfo = myInfo;
}-*/;
// called onload
@Override
public void onModuleLoad() {
// add function to window object
myInfo();
}
public static void myJavaInfo(String value) {
Info.display("Works", "From Javascript: " + value);
}
}
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>GXT Project 4.0</title>
<!-- The project.gwt.xml entrypoint -->
<!-- <script type="text/javascript" src="MyGxtProject40/MyGxtProject40.nocache.js"></script> -->
</head>
<body>
<script>
startApp = () => {
// Instead import the javascript entrypoint at anytime.
let st = document.createElement("script");
st.type = "text/javascript";
st.src = "./MyGxtProject40/MyGxtProject40.nocache.js";
document.getElementsByTagName('head')[0].appendChild(st)
}
</script>
<!-- Manually import the GWT entrypoint in the future -->
<!-- This will call onLoad -->
<input type='button' value='Start app' onclick="startApp();"></input>
<br/>
<br/>
<input type='button' value='Test info' onclick="window.myInfo('Hello')"></input>
</body>
</html>
@branflake2267
Copy link
Author

screen shot 2019-02-22 at 10 16 40 am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment