CheerpJ allows users to interact with the browser DOM directly from Java, without overhead. To achieve this you will need to use the CheerpJ Java API (
cheerpj-dom.jar) and found at the CheerpJ downloadable archive. Usage examples are shown below.
JSString name is used for the JS version. The static
Global.JSString utility function can be used to create
JSStrings from Java
Strings. If a
JSString needs to be used many times it could be useful to cache it. Similarly the
Global.JavaString function can be used to convert back from
JSString to normal Java
String. See Global.JSString and Global.JavaString.
Calling JS methods
Global class provides a few static methods that can be used to call arbitrary JS functions in the global scope, See Global.jsCall
Building the code
Assuming the example above is contained in
DomExample.java, you need to first build the program using the standard
javac compiler, create a JAR and then create the JAR.JS from it using CheerpJ. In both steps the
cheerpj-dom.jar must be explicitly added to the command line as a dependency.
- Java API (Reference)