How to pass ArrayList in JSNI

671 views
Skip to first unread message

minahMalaysia

unread,
May 8, 2008, 12:33:49 AM5/8/08
to Google Web Toolkit
Greetings,

I want to pass an Arraylist from a java class to java applet via JSNI
(JSNI does nothing to the arraylist,just pass it to applet).
I'm wondering how to do that ? Because i keep getting js error msg .
How to make arraylist usable in JSNI?

thanks...

Hiedi

unread,
May 8, 2008, 12:16:29 PM5/8/08
to Google Web Toolkit
You have to convert the arrayList into a javascript object.

import com.google.gwt.core.client.JavaScriptObject;

public class ArrayHelper {

public static JavaScriptObject arrayConvert(Object[] array) {
JavaScriptObject result = newJSArray(array.length);
for (int i = 0; i < array.length; i++) {
arraySet(result, i, array[i]);
}
return result;

}

private static native JavaScriptObject newJSArray(int length) /*-{
if (length < 0)
{
return new Array();
}
else
{
return new Array(length);
}


}-*/;

public static native int arrayLength(JavaScriptObject array) /*-{
return array.length;


}-*/;

public static native Object arrayGetObject(JavaScriptObject array,
int index) /*-{
return array[index];


}-*/;

public static native void arraySet(JavaScriptObject array, int index,
Object value) /*-{
array[index] = value;

}-*/;

}

Olivier Monaco

unread,
May 8, 2008, 8:25:52 AM5/8/08
to Google Web Toolkit
Hi,

Remembre that GWT compile you Java class to JavaScript. So your
ArrayList is a JavaScript object and has nothing similar the Java
class ArrayList...

To use ArrayList object within JSNI, you can use read
http://code.google.com/webtoolkit/documentation/com.google.gwt.doc.DeveloperGuide.JavaScriptNativeInterface.html#JavaFromJavaScript.

Olivier.

minahMalaysia

unread,
May 8, 2008, 11:41:25 PM5/8/08
to Google Web Toolkit
but i don't think i can used that to call java applet right??


On May 8, 8:25 pm, Olivier Monaco <olivier.mon...@free.fr> wrote:
> Hi,
>
> Remembre that GWT compile you Java class to JavaScript. So yourArrayListis a JavaScript object and has nothing similar the Java
> classArrayList...
>
> To useArrayListobject within JSNI, you can use readhttp://code.google.com/webtoolkit/documentation/com.google.gwt.doc.De....
>
> Olivier.
>
> On 8 mai, 06:33, minahMalaysia <equinocks...@gmail.com> wrote:
>
> > Greetings,
>
> > I want topassanArraylistfrom a java class to java applet via JSNI
> > (JSNI does nothing to thearraylist,justpassit to applet).

Olivier Monaco

unread,
May 11, 2008, 4:24:48 AM5/11/08
to Google Web Toolkit
Hi,

Your problem is more "how to pass JavaScript objects to Java Applet?".
May be http://java.sun.com/j2se/1.4.2/docs/guide/plugin/developer_guide/java_js.html
(I don't write applets)...

Olivier.
Reply all
Reply to author
Forward
0 new messages