clearing or preventing double-click selection

2,361 views
Skip to first unread message

decitrig

unread,
Sep 12, 2010, 3:29:53 PM9/12/10
to Google Web Toolkit
I'm working on an application that responds to double clicks on text
labels, and I'd like to either disable double-click text selection or
clear it from within the event handler. I tried $doc.selection.clear()
in a native method, but apparently $doc.selection is "not an object" -
it may be that the selection isn't occurring until after the double-
click handler is getting called. I did try stopPropagation and
preventDefault, neither helped.

Any pointers?

--
decitrig

lineman78

unread,
Sep 13, 2010, 3:08:08 PM9/13/10
to Google Web Toolkit
In your onClick method you can call event.preventDefault() to keep the
browser from handling the event.

decitrig

unread,
Sep 13, 2010, 3:43:11 PM9/13/10
to Google Web Toolkit
On Sep 13, 3:08 pm, lineman78 <linema...@gmail.com> wrote:
> In your onClick method you can call event.preventDefault() to keep the
> browser from handling the event.

I already tried that in the doubleclick handler, no luck. Just
implemented it in a single-click handler, still no luck.

Thomas Broyer

unread,
Sep 14, 2010, 5:53:06 AM9/14/10
to Google Web Toolkit
There's a CSS property that you can use to disable text selection:
user-select.
It's available in WebKit (-webkit-user-select) and Firefox (-moz-user-
select). I can't tell for Opera (-o-user-select?).
In IE, you'd have to listen to an onselectionstart event and cancel
it.

lineman78

unread,
Sep 14, 2010, 12:41:51 PM9/14/10
to Google Web Toolkit
This works for preventing all selection on a widget.

addDomHandler(new MouseDownHandler()
{
@Override
public void onMouseDown(MouseDownEvent event)
{
event.preventDefault();
}
}, MouseDownEvent.getType());

David Chandler

unread,
Sep 14, 2010, 3:13:14 PM9/14/10
to Google Web Toolkit
This will disable text selection altogether:

protected native static void disableTextSelectInternal(Element e,
boolean disable)
/*-{
if (disable) {
e.ondrag = function () { return false; };
e.onselectstart = function () { return false; };
e.style.MozUserSelect="none"
} else {
e.ondrag = null;
e.onselectstart = null;
e.style.MozUserSelect="text"
}
}-*/;

/dmc
http://turbomanage.wordpress.com
Reply all
Reply to author
Forward
0 new messages