views:

2542

answers:

1

I have a native Javascript method in one of my GWT Java classes, but I'm having trouble calling my Java methods from the native Javascript code. I tried to follow this as closely as I could, but I can't get it to work. I compiled it and ran it in Firefox, and the error console said "Error: this.lc is not a function". I tried changing all the methods to public, but that didn't seem to make a difference. What am I doing wrong?

package com.proprintsgear.design_lab.client;
...
public class ValueBox extends HorizontalPanel {
...
private void fireChange() {
    ...
}

private void increaseValue() {
    ...
}

private native void addNativeMouseWheelListener(String id) /*-{
    function mouseOverHandler(e) {
     $wnd.addEventListener("DOMMouseScroll", scrollWheelMove, false);
    }

    function mouseOutHandler(e) {
     $wnd.removeEventListener("DOMMouseScroll", scrollWheelMove, false);
    }

    function scrollWheelMove(e) {
     if ($wnd.event || $wnd.Event) {
      if (!e) e = $wnd.event;
      if (e.wheelDelta <= 0 || e.detail > 0 ) {
       $wnd.alert("DOWN");
      } else {
       [email protected]_lab.client.ValueBox::increaseValue()();
      }
      [email protected]_lab.client.ValueBox::fireChange()();
     }
    }

    var box=$doc.getElementById(id);
    box.addEventListener("mouseout",mouseOutHandler,false);
    box.addEventListener("mouseover",mouseOverHandler,false);
}-*/;
+1  A: 

In all the code I've done in the past, I've never used 'this' to identify my class, I have passed the class in.

Eg: Change this:

private native void addNativeMouseWheelListener(String id) /*-{
    function mouseOverHandler(e) {
        $wnd.addEventListener("DOMMouseScroll", scrollWheelMove, false);
    }

    function mouseOutHandler(e) {
        $wnd.removeEventListener("DOMMouseScroll", scrollWheelMove, false);
    }

    function scrollWheelMove(e) {
        if ($wnd.event || $wnd.Event) {
                if (!e) e = $wnd.event;
                if (e.wheelDelta <= 0 || e.detail > 0 ) {
                        $wnd.alert("DOWN");
                } else {
                        [email protected]_lab.client.ValueBox::increaseValue()();
                }
                [email protected]_lab.client.ValueBox::fireChange()();
        }
    }

    var box=$doc.getElementById(id);
    box.addEventListener("mouseout",mouseOutHandler,false);
    box.addEventListener("mouseover",mouseOverHandler,false);
}-*/;

To this:

private native void addNativeMouseWheelListener(ValueBox instance, String id) /*-{
    function mouseOverHandler(e) {
        $wnd.addEventListener("DOMMouseScroll", scrollWheelMove, false);
    }

    function mouseOutHandler(e) {
        $wnd.removeEventListener("DOMMouseScroll", scrollWheelMove, false);
    }

    function scrollWheelMove(e) {
        if ($wnd.event || $wnd.Event) {
                if (!e) e = $wnd.event;
                if (e.wheelDelta <= 0 || e.detail > 0 ) {
                        $wnd.alert("DOWN");
                } else {
                        [email protected]_lab.client.ValueBox::increaseValue()();
                }
                [email protected]_lab.client.ValueBox::fireChange()();
        }
    }

    var box=$doc.getElementById(id);
    box.addEventListener("mouseout",mouseOutHandler,false);
    box.addEventListener("mouseover",mouseOverHandler,false);
}-*/;
rustyshelf
Perfect! This makes sense, but I wish it was documented better on the GWT site.
DLH