Defining Listener methods in Groovy

Hi,
I’ve been experimenting with using Groovy and Vaadin, with the Groovy Addon. It works really, really well. Also Groovy is definitely a less verbose programming language than Java.

I’ve run into a question / problem if anyone familiar with Vaadin & Groovy could help would be wonderful.

I’ve already got a click listener for the Button “settings” defined like this:

      settings.addListener(new ClickListener ()
            {
               void buttonClick(ClickEvent event)
               {
               settings()
               }
            }
            )

Which works fine.

I tried this:

settings.buttonClick = {event ->  settings() }

and I got a "
buttonClick property is not defined for com.vaadin.ui.Button
" exception. However, the
Groovy documentation
clearly states this is the “correct” approach.

and then looking at the options the Eclipse Groovy plugin gave me, I tried this:

settings.BUTTON_CLICK_METHOD = {event ->  settings() }

Still get the same property exception. Anyone have any pointers on what I’m doing wrong? Or must I use the first (ugly) Java-esque approach?
Thanks in advance,
Anthony

Hi,

you should try this,


settings.addClickListener({ event -> 
   //do something			 
} as ClickListener) 

“as ClickListener” is the key…

Thanks Şeref for your reply, my code looks like this:

settings.addClickListener ( { event -> log() } as ClickListener)

But I am still getting this error:

   No signature of method: com.vaadin.ui.Button.addClickListener() is applicable for argument types: (sun.proxy.$Proxy4) values: [com.fyrefrog.wordcrackr.GamePage$_init_closure1@17fd2c3]

Caused by:

groovy.lang.MissingMethodException: No signature of method: com.vaadin.ui.Button.addClickListener() is applicable for argument types: (sun.proxy.$Proxy4) values: [com.fyrefrog.wordcrackr.GamePage$_init_closure1@17fd2c3]

Followed your structure as far as I can tell, any suggestions as to what I may be doing wrong?

Hi Anthony,


No signature of method: com.vaadin.ui.Button.addClickListener() is applicable for argument types: ........

I guess you use different ClickListener interface, its belongs to other component etc. Can you check import statements

import com.vaadin.ui.Button.ClickListener

or just use as:

settings.addClickListener ( { event -> log() } as Button.ClickListener)