How to distinguish click and double click in the same widget?

64 views
Skip to first unread message

ARK

unread,
Oct 21, 2006, 1:24:58 PM10/21/06
to Google Web Toolkit
Hi all,

I think "this.sinkEvents(Event.ONDBLCLICK|Event.ONCLICK);" desn't
work.

I set a break-point in "onBrowserEvent", no matter I click or
doubleClick, the "DOM.eventGetType(event)" always be 1 that means
"Event.ONCLICK".

I tried to sink mousedown or mouseup along with
Event.ONDBLCLICK|Event.ONCLICK, but double-click is never sinked to
"onBrowserEvent". Only under one situation this could work:

Only sink the Event.ONDBLCLICK without Event.ONCLICK.

Doesn't anybody meet this problem before? is it a bug?

Ark

dominik_steiner

unread,
Oct 24, 2006, 3:04:51 AM10/24/06
to Google Web Toolkit
Hi Ark,

that is a good point. If you don't set a breakpoint but just traces you
will see that first to Event.ONCLICK are sent and after them one
Event.ONDBLCLICK.

This leads to the problem that you can't react only to double clicks
efficiently because you always get a single click before. Lets say you
have a table with a label which represents a link and is triggering the
link when a single click is executed on it. But you want to be able to
edit the link by doing a dbl click on it. I haven't found a way how to
solve that?

Does anybody have a workaround for that?

Dominik

Ian Bambury

unread,
Oct 24, 2006, 8:13:19 AM10/24/06
to Google-We...@googlegroups.com
The only way I can think of to react to a double-click and not a single-click is:
 
In the constructor, set up a timer to run a method which performs the single-click processing
 
In the single-click event, schedule the timer to run after the time allowed for it to be a double-click
 
In the double-click event, cancel the timer.
 
HTH
 
Ian

dominik_steiner

unread,
Oct 25, 2006, 2:30:01 AM10/25/06
to Google Web Toolkit
Hi Ian,

thanks for your answer. I hadn't thought about that workaround, but it
sounds good. I hope that the GWT team might solve that issue in a more
coherent way.

Thanks for your help, anyway.

Dominik

Jason Essington

unread,
Oct 25, 2006, 10:35:50 AM10/25/06
to Google-We...@googlegroups.com
I suppose there could be a feature request here.

I have had to create my own click listener (and added code to
onBrowserEvent) that is capable of
discerning leftClick, rightClick, shiftClick, ctrlClick, and
dblClick. It works like a charm

-jason

Thad Humphries

unread,
Oct 25, 2006, 10:54:29 AM10/25/06
to Google-We...@googlegroups.com
Is this available for inspection or downloading anywhere?

On Wednesday 25 October 2006 10:35, Jason Essington wrote:
> I suppose there could be a feature request here.
>
> I have had to create my own click listener (and added code to  
> onBrowserEvent) that is capable of
> discerning leftClick, rightClick, shiftClick, ctrlClick, and  
> dblClick. It works like a charm

--
"It is awfully easy to be hard-boiled about everything in the daytime, but
at night it is another thing." -- Ernest Hemingway, 'The Sun Also Rises'

matias_warrior

unread,
Nov 19, 2006, 12:04:44 PM11/19/06
to Google Web Toolkit
Hi, I implemented the solution posted by Ian Bambury, of using a timer
at the onClick() event.
I redefined a Label so that it can be able to distinguish between a
simple click and a doble click.
It ´s work great!!
Here is the implementation

/**
* Label que sobrescribe el metodo onBrowserEvent para mandarle el tipo
de
* evento
*
* @author Matias Leone
*
*/
public class LabelTeg extends Label {

private static final int TIMER = 40;

// Atributos que no me quedaba otra que redefinir
private ClickListenerCollection clickListeners;
private MouseListenerCollection mouseListeners;
private DoubleClickListenerTeg clickListenerTeg;
private Timer timer;
private int clicksCount;

// ==========================//
public LabelTeg() {
super();
sinkEvents( Event.ONDBLCLICK);
clicksCount = 0;
timer = new DoubleClickTimer( this );
}
// ==========================//
/**
* Creates a label with the specified text.
*
* @param text the new label's text
*/
public LabelTeg(String text) {
this();
setText(text);
}
// ==========================//
/**
* Creates a label with the specified text.
*
* @param text
* the new label's text
* @param wordWrap
* <code>false</code> to disable word wrapping
*/
public LabelTeg(String text, boolean wordWrap) {
this(text);
setWordWrap(wordWrap);
}
// ==========================//
/**
* Metodo redefinido para poder captar eventos de doble click sobre un
label
*
*/
public void onBrowserEvent(Event event) {
switch (DOM.eventGetType(event)) {
case Event.ONDBLCLICK:
clickListenerTeg.onDoubleClick(this);
return;
//Si fue click, vemos si corresponde a un doble click, sino
iniciamos un timer
case Event.ONCLICK:
if (this.clickListenerTeg != null) {
clicksCount++;
if ( clicksCount >= 2 ) {
timer.cancel();
clicksCount = 0;
} else {
timer.scheduleRepeating( TIMER );
}
}
break;
case Event.ONMOUSEDOWN:
case Event.ONMOUSEUP:
case Event.ONMOUSEMOVE:
case Event.ONMOUSEOVER:
case Event.ONMOUSEOUT:
if (mouseListeners != null)
mouseListeners.fireMouseEvent(this, event);
break;
}
}
// ==========================//
/**
* Atiende un evento de mouse que sea solo un click comun, y no un
doble click
*/
protected void atenderClickComun() {
timer.cancel();
clicksCount = 0;
if (clickListeners != null) {
clickListeners.fireClick(this);
}
}
// ==========================//
/**
* Agrega un captador de eventos del mouse para el Teg
*/
public void addDoubleClickListenerTeg(DoubleClickListenerTeg
mouseListener) {
this.clickListenerTeg = mouseListener;
}

// ==========================//
/**
* Quita un captador de eventos del mouse para el Teg
*/
public void removeDoubleClickListenerTeg(
DoubleClickListenerTeg mouseListener) {
this.clickListenerTeg = null;
}
// ==========================//

/* Metodos que no me quedaba otra que redefinir */

// ==========================//
public void removeClickListener(ClickListener listener) {
if (clickListeners != null)
clickListeners.remove(listener);
}

public void removeMouseListener(MouseListener listener) {
if (mouseListeners != null)
mouseListeners.remove(listener);
}

public void addClickListener(ClickListener listener) {
if (clickListeners == null)
clickListeners = new ClickListenerCollection();
clickListeners.add(listener);
}

public void addMouseListener(MouseListener listener) {
if (mouseListeners == null)
mouseListeners = new MouseListenerCollection();
mouseListeners.add(listener);
}
// ==========================//
/**
* Clase privada para el timer
*/
private class DoubleClickTimer extends Timer {

private LabelTeg labelTeg;

// ==========================//
public DoubleClickTimer(LabelTeg labelTeg) {
this.labelTeg = labelTeg;
}
// ==========================//
/**
* Avisa que fue un click comun
*/
public void run() {
labelTeg.atenderClickComun();
}
}
// ==========================//

}

This is the interface that the container of my Label must implement.

public interface DoubleClickListenerTeg {

/**
* Fired when the user double clicks on a widget.
*
* @param sender the widget sending the event.
*/
public void onDoubleClick(Widget sender);

}

Reply all
Reply to author
Forward
0 new messages