开发者

GWT JSNI BOOLEAN

开发者 https://www.devze.com 2023-03-11 21:24 出处:网络
Here\'s my code: package com.eggproject_hu.WPECommerceAdminSales.client; import java.lang.Boolean; import com.google.gwt.core.client.GWT;

Here's my code:

package com.eggproject_hu.WPECommerceAdminSales.client;

import java.lang.Boolean;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;

public class AblakVillogo
{
    public static Boolean focusedWindow = true;
    private static Boolean init = false;

    public static void setFocused(Boolean focus)
    {
        focusedWindow = focus;
    }

    public static Boolean getFocused()
    {
        return focusedWindow;
    }

    public static void focusVizsgalat()
    {
        if(focusedWindow == true)
        {
            GWT.log("igen");
        }
        else
        {
            GWT.log("nem");
        }
    }

    public static void init()
    {
        if(init == false)
        {
            _init();
        }
    }

    private native static void _init() /*-{
        $wnd.jQuery(document).ready(function()
        {
            $wnd.jQuery($wnd).focus(function()
            {
@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::focusVizsgalat()();               @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(true);
                $wnd.console.log("focus");
            }).blur(function()
            {
                var ret = false;
                @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::focusVizsgalat()();
                                @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolea开发者_高级运维n;)(false);
                $wnd.console.log("blur");
            });
        });
    }-*/;
}

I see this in the browser console:

uncaught exception: java.lang.IllegalArgumentException: invoke arguments: JS value of type boolean, expected java.lang.Boolean

I've tested in Chrome and Firefox.

What is the problem?

Thanks for the help!


You have to declare the Boolean as the primitive boolean to set the value from javascript

and you dont need to specifiy L/java/lang/Boolean in the call but Z instead


Either follow Daniel's advice, but then you have to change your method to take a boolean argument (i.e. use boolean all the way through), or you can explicitly cast/box your boolean in a java.lang.Boolean in your JSNI method:

@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(@java.lang.Boolean::valueOf(Z)(true));

…even though in your case, because the value is a constant, I'd rather directly use the Boolean constants TRUE and FALSE:

@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(@java.lang.Boolean::TRUE);

That being said, I do believe Daniel's advice is your best fit.

0

精彩评论

暂无评论...
验证码 换一张
取 消