I want to handle events when user pastes some text in TextBox
. Which event is fired in this situation? I tried ValueChange
and Change
handlers, but they didn't work.
Asked
Active
Viewed 1.0k times
7

ryskajakub
- 6,351
- 8
- 45
- 75
3 Answers
9
This might help you. Describes a workaround to hook to the onpaste event. In short:
subclass
TextBox
sink the onpaste event in the constructor
sinkEvents(Event.ONPASTE);
override
onBrowserEvent(Event event)
public void onBrowserEvent(Event event) { super.onBrowserEvent(event); switch (event.getTypeInt()) { case Event.ONPASTE: { // do something here break; } } }

z00bs
- 7,518
- 4
- 34
- 53
-
Link is dead. I assume it was meant to be http://groups.google.com/group/google-web-toolkit/browse_thread/thread/5d9a6fbe9e2bacf2 – alexandroid Nov 08 '11 at 02:50
-
I know this is old now. I'm commenting this because I'm hitting a situation, not sirous, but worth some awareness. My box is sopposed to be numeric, so I'm adding onclick, onblur and the sink for onpaste. Thing is, with on paste, the click and blur events are not fired from the handlers I added, rather they are fired on the onBrowser event. Why is this? – Nuno Gonçalves Apr 24 '12 at 10:52
-
I meant KeyUp, not click event. ;) – Nuno Gonçalves Apr 24 '12 at 10:53
-
Hi z00bs, can you please give the overall code, how it fits together. I mean, where do we attach the `onBrowser` and `sinkEvent` methods? – SexyBeast Jan 28 '13 at 08:39
-
`sinkEvent` is called in the constructor of the widget (as I wrote) and the `onBrowserEvent()` method you don't need to 'attach' just overwrite it to handle the newly sunk event. It's being called by GWT if any of the sunk events is triggered. – z00bs Feb 06 '13 at 07:19
7
GWT does not yet have support for cut, copy & paste: http://code.google.com/p/google-web-toolkit/issues/detail?id=4030
Edited: Another option is to use JSNI. For example add this to your GWT class:
public native void addCutHandler(Element element)
/*-{
var temp = this; // hack to hold on to 'this' reference
element.oncut = function(e) {
temp.@org.package.YourClass::handleCut()();
}
}-*/;
public void handleCut() {
Window.alert("Cut!");
}

ᴇʟᴇvᴀтᴇ
- 12,285
- 4
- 43
- 66

Peter Knego
- 79,991
- 11
- 123
- 154
-
I combined this answer with the following to get textbox content after a paste: https://stackoverflow.com/questions/13895059/how-to-alert-after-paste-event-in-javascript. I also wanted to add that I prefer this method because it seems simpler to me than extending a class. – Jake88 Feb 20 '19 at 19:27
1
**(Write In the Constructor)**
sinkEvents( Event.ONPASTE );
**(After that write below code)**
public void onBrowserEvent( Event event )
{
super.onBrowserEvent( event );
switch ( event.getTypeInt() )
{
case Event.ONPASTE :
{
event.stopPropagation();
event.preventDefault();
break;
}
}
}

rohith
- 11
- 1