How to sink Event.ONCLICK and Event.ONDBLCLICK

2,613 views
Skip to first unread message

Ewa Maciaś

unread,
Apr 30, 2008, 12:50:36 PM4/30/08
to Google-We...@googlegroups.com
Hi,

I am trying to create button that will react on click and double click.
My first try is:

public class DoubleClickButton extends Button {
 
  public DoubleClickButton() {
    super();
    sinkEvents(Event.ONCLICK);
    sinkEvents(Event.ONDBLCLICK);
  }


  @Override
  public void onBrowserEvent(Event e) {
    super.onBrowserEvent(e);
    if (DOM.eventGetType(e) == Event.ONCLICK) {
      Window.alert("Single click");
    }
    if (DOM.eventGetType(e) == Event.ONDBLCLICK) {
      Window.alert("Double click");
    }
  }


The problem is that the button always displays "Single click" alert.
When I remove sinkEvents(Event.ONCLICK); from constructor it displays "Double click" alert on double click.

Any ideas?

Ewa

Folke

unread,
Apr 30, 2008, 8:40:36 PM4/30/08
to Google Web Toolkit
Event types are stored as a bit field. You have combine both types
with bit-wise OR:

sinkEvents(Event.ONCLICK | Event.ONDBLCLICK);


On Apr 30, 6:50 pm, "Ewa Maciaś" <ewa...@gmail.com> wrote:
> Hi,
>
> I am trying to create button that will *react on click and double click*.

Ewa Maciaś

unread,
May 5, 2008, 10:58:45 AM5/5/08
to Google-We...@googlegroups.com
Hi,

Thanks for replay.
Unfortunately your solution is not correct.

sinkEvents(Event.ONCLICK);
sinkEvents(Event.ONDBLCLICK);
is exactly the same as
sinkEvents(Event.ONCLICK | Event.ONDBLCLICK);

take a look at implementation of void com.google.gwt.user.client.ui.UIObject.sinkEvents(int eventBitsToAdd) :

public void sinkEvents(int eventBitsToAdd) {
    DOM.sinkEvents(getElement(), eventBitsToAdd
        | DOM.getEventsSunk(getElement()));
  }

So it works exactly the same.
Any other ideas?

Ewa

2008/5/1 Folke <mes...@gmail.com>:

Carl Scott

unread,
May 5, 2008, 5:18:15 PM5/5/08
to Google Web Toolkit
You know how some operating systems have a setting where you can
increase or decrease the double-click response rate, so for those who
don't double click so fast, it'll accept it with longer rest periods
between clicks? You need something like that. Because a double click
is nothing more than two single clicks, and it will be registered like
that in event handling.

Something simple like so:

Timer t = new Timer() { public void run() { } };;
public void onBrowserEvent(Event evt) {
switch(DOM.eventGetType(evt)) {
case Event.ONDBLCLICK:
t.cancel();
//Do something related to double click
break;
case Event.ONCLICK: {
t.cancel();
t = new Timer() {
public void run() {
//Do something related to single click
}
};
t.schedule(500); //num milliseconds before you decide double
click is out of the question
break;
}
default:
super.onBrowserEvent(evt);
}
}

***REAALLY* though, for most cases, you shouldn't implement both
single & double click on a single object, it's just bad taste. But if
you have to, that is *a* solution (not the single definitive one) that
should work.

--
Carl Scott
Software Developer, Solertium Corporation

On May 5, 10:58 am, "Ewa Maciaś" <ewa...@gmail.com> wrote:
> Hi,
>
> Thanks for replay.
> Unfortunately your solution is not correct.
> *
> sinkEvents(Event.ONCLICK);
> sinkEvents(Event.ONDBLCLICK);
> * is exactly the same as
> *sinkEvents(Event.ONCLICK | Event.ONDBLCLICK);*
>
> take a look at implementation of void
> com.google.gwt.user.client.ui.UIObject.sinkEvents(int eventBitsToAdd) :
>
> public void sinkEvents(int eventBitsToAdd) {
> DOM.sinkEvents(getElement(), eventBitsToAdd
> | DOM.getEventsSunk(getElement()));
> }
>
> So it works exactly the same.
> Any other ideas?
>
> Ewa
>
> 2008/5/1 Folke <mess...@gmail.com>:

walden

unread,
May 6, 2008, 8:00:14 AM5/6/08
to Google Web Toolkit
I agree with Carl: bad style.

But if you're hell-bent on it, I'd suggest trying to get a simple case
to work in pure Javascript first. If you can't do that, bag it.

Walden
> > > > Ewa- Hide quoted text -
>
> - Show quoted text -

Olivier Monaco

unread,
May 7, 2008, 6:16:46 AM5/7/08
to Google Web Toolkit
Hi,

Do you know that a double click is twice click? so a ONCLICK event is
fired on the first click and then a ONDBLCLICK is fired on the second
click. So if you show a alert box in the ONCLICK, you can't click a
second time. So you have only a ONCLICK event...

Try to use GWT.log in place of Window.alert and see...

Or something like
@Override public void onBrowserEvent(Event e)
{
super.onBrowserEvent(e);
if (DOM.eventGetType(e) == Event.ONCLICK) {
RootPanel.get().add(new Label("Single click"));
}
if (DOM.eventGetType(e) == Event.ONDBLCLICK) {
RootPanel.get().add(new Label("Double click"));
}
}

Olivier.

On May 5, 4:58 pm, "Ewa Maciaś" <ewa...@gmail.com> wrote:
> Hi,
>
> Thanks for replay.
> Unfortunately your solution is not correct.
> *
> sinkEvents(Event.ONCLICK);
> sinkEvents(Event.ONDBLCLICK);
> * is exactly the same as
> *sinkEvents(Event.ONCLICK | Event.ONDBLCLICK);*
>
> take a look at implementation of void
> com.google.gwt.user.client.ui.UIObject.sinkEvents(int eventBitsToAdd) :
>
> public void sinkEvents(int eventBitsToAdd) {
> DOM.sinkEvents(getElement(), eventBitsToAdd
> | DOM.getEventsSunk(getElement()));
> }
>
> So it works exactly the same.
> Any other ideas?
>
> Ewa
>
> 2008/5/1 Folke <mess...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages