Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm creating a GWT wrapper round a JavaScript library. One of the JavaScript functions takes an anonymous object as its argument e.g.:

obj.buildTabs({ hide: true, placeholder: 'placeholder' });

On the Java side how do I create this type of JavaScript object and pass it to my native implementation?

At the moment, on the Java side I have:

public void buildTabs(TabConfiguration config) {
   // ?
}

private native void buildTabs(?) /*-{
    	$wnd.NAMESPACE.lib.buildTabs(?);
}-*/;

Any pointers appreciated, thanks.

share|improve this question

2 Answers

up vote 2 down vote accepted

if you exactly know what parameters should be used, you can do the following (remove additional new lines after ::)

private native void buildTabs(TabConfiguration config) /*-{
        $wnd.NAMESPACE.lib.buildTabs({hide: 
                [email protected]::
                getHide()(), 
                placeholder: 
                [email protected]::
                getPlaceholder()()});
}-*/;

a small clip from the GWT documentation:

public native void bar(JSNIExample x, String s) /*-{
    // Call instance method instanceFoo() on this
    [email protected]::instanceFoo(Ljava/lang/String;)(s);

    // Call instance method instanceFoo() on x
    [email protected]::instanceFoo(Ljava/lang/String;)(s);

    // Call static method staticFoo()
    @com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);

    // Read instance field on this
    var val = [email protected]::myInstanceField;

    // Write instance field on x
    [email protected]::myInstanceField = val + " and stuff";

    // Read static field (no qualifier)
    @com.google.gwt.examples.JSNIExample::myStaticField = val + " and stuff";
  }-*/;
share|improve this answer

Refer: http://code.google.com/eclipse/docs/gwt%5Fjsni.html It describes how you can pass parameters between JavaScript and Java world. Also check out Google Plugin for Eclipse, if you find yourself working a lot with JSNI

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.