Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT JSNI BOOLEAN

Tags:

gwt

jsni

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/Boolean;)(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!

like image 743
Gabor Harangozo Avatar asked Aug 03 '26 18:08

Gabor Harangozo


2 Answers

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

like image 70
Daniel Kurka Avatar answered Aug 07 '26 02:08

Daniel Kurka


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.

like image 22
Thomas Broyer Avatar answered Aug 07 '26 03:08

Thomas Broyer