Programatically clicking a checkbox

12 views
Skip to first unread message

byhisdeeds

unread,
May 25, 2008, 9:53:01 AM5/25/08
to Google Web Toolkit
Can some one say how I can programatically call the click listener
attached to a checkbox. When I call setCheck(true) the checkbox gets
set but the click listener doesn't get called.

John

Ian Bambury

unread,
May 25, 2008, 10:04:58 AM5/25/08
to Google-We...@googlegroups.com
Depends - if you have a named listener, it's just
 
listener.onClick(myCheckBox);
 
Ian

2008/5/25 byhisdeeds <byhis...@gmail.com>:



--
Ian
http://examples.roughian.com
__________________________________________

Stuff the environment - print this email
__________________________________________

Reinier Zwitserloot

unread,
May 25, 2008, 10:31:29 AM5/25/08
to Google Web Toolkit
Basically, you can't. If you have a reference to the listener, you can
fire it manually.

However, if you must, there's this unsupported hack, which will likely
keep working until a major new GWT release that has something about
'overhauled the event system' in it. Until then, enjoy:

public static native void fakeClick(FocusWidget widget) /*-{
var cls =
widget.@com.google.gwt.user.client.ui.FocusWidget::clickListeners;
if (!cls) return;

cls.@com.google.gwt.user.client.ui.ClickListenerCollection::fireClick(Lcom/
google/gwt/user/client/ui/Widget;)(widget);
}-*/;

I know, looks magic. Read up on "JSNI" (google lucky that) and realize
that in JSNI, 'private', 'final', and other such java distinctions no
longer exist and you are free to call whatever you want.

usage:

fakeClick(someButton);
fakeClick(anyFocusWidgetSubclass);

Reinier Zwitserloot

unread,
May 25, 2008, 10:32:21 AM5/25/08
to Google Web Toolkit
because of google groups email filtering, that may show up as widg...
instead of 'widget', and 'c...' instead of 'cls'. Both of those before
the at sign.

On May 25, 4:31 pm, Reinier Zwitserloot <reini...@gmail.com> wrote:
> Basically, you can't. If you have a reference to the listener, you can
> fire it manually.
>
> However, if you must, there's this unsupported hack, which will likely
> keep working until a major new GWT release that has something about
> 'overhauled the event system' in it. Until then, enjoy:
>
> public static native void fakeClick(FocusWidget widget) /*-{
>    var cls =
> widg...@com.google.gwt.user.client.ui.FocusWidget::clickListeners;
>    if (!cls) return;
>
> c...@com.google.gwt.user.client.ui.ClickListenerCollection::fireClick(Lcom/

byhisdeeds

unread,
May 29, 2008, 3:24:04 PM5/29/08
to Google Web Toolkit
Thanks.

John

On May 25, 9:32 am, Reinier Zwitserloot <reini...@gmail.com> wrote:
> because of google groups email filtering, that may show up as widg...
> instead of 'widget', and 'c...' instead of 'cls'. Both of those before
> the at sign.
>
> On May 25, 4:31 pm, Reinier Zwitserloot <reini...@gmail.com> wrote:
>
> > Basically, you can't. If you have a reference to the listener, you can
> > fire it manually.
>
> > However, if you must, there's this unsupported hack, which will likely
> > keep working until a major new GWT release that has something about
> > 'overhauled the event system' in it. Until then, enjoy:
>
> > public static native void fakeClick(FocusWidget widget) /*-{
> > var cls =
> > widg...@com.google.gwt.user.client.ui.FocusWidget::clickListeners;
> > if (!cls) return;
>
> > c...@com.google.gwt.user.client.ui.ClickListenerCollection::fireClick(Lcom/
> > google/gwt/user/client/ui/Widget;)(widget);
>
> > }-*/;
>
> > I know, looks magic. Read up on "JSNI" (google lucky that) and realize
> > that in JSNI, 'private', 'final', and other such java distinctions no
> > longer exist and you are free to call whatever you want.
>
> > usage:
>
> > fakeClick(someButton);
> > fakeClick(anyFocusWidgetSubclass);
>

Peter Blazejewicz

unread,
May 29, 2008, 4:52:00 PM5/29/08
to Google Web Toolkit
hi John,

no hacks, just using non-implemented "click" method from html
input[checkbox] element:

package com.mycompany.project.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class CHBTest implements EntryPoint, ClickListener {
// chbx
private CheckBox chb;

public void onModuleLoad() {
chb = new CheckBox("label");
// we do have listeners, do we?
chb.addClickListener(this);
// repeat for visual effect
new Timer() {
public void run() {
doClick(chb.getElement().getFirstChildElement());
}
}.scheduleRepeating(1000);
RootPanel.get().add(chb);
}

native void doClick(Element elem)
/*-{
if(elem && elem.click){
elem.click();
}
}-*/;

public void onClick(Widget sender) {
chb.setText(chb.isChecked() ? "checked" : "unchecked");
}
}


see bizarre "getElement()", that's because GWT checkbox consists of 3
widgets (parent +subelements), and actual input[checkbox] element is
subelement,
"click" is implemented in GWT ui button widget but not in other
widgets,

regards,
Peter

On May 25, 3:53 pm, byhisdeeds <byhisde...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages