I wonder how it is possible to listen for the drop event for a TextField
. I need to intercept that events in order to do value checks.
I wrote an extended TextBox
, which fires TextChangeEvents
. But when i drop a text into the text field, I don't get a call for the onBrowserEvent(Event event)
method.
Any hint?
public class ExtendedTextBox extends TextBox {
/**
* Creates an empty extended text box.
*/
public ExtendedTextBox() {
super();
// Catch the browser events.
sinkEvents(Event.ONKEYUP);
sinkEvents(Event.ONPASTE);
}
@Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
System.out.println(event.getTypeInt());
switch (event.getTypeInt()) {
case Event.ONKEYUP:
case Event.ONPASTE: {
// Fire the event after the text box shows the new data.
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
fireEvent(new TextChangeEvent(ExtendedTextBox.this.getText()));
}
});
break;
}
default:
}
}
/**
* Add a given {@link TextChangeEventHandler} to the widget.
*
* @param handler
* the handler
* @return {@link HandlerRegistration} used to remove the handler
*/
public HandlerRegistration addTextChangeEventHandler(TextChangeEventHandler handler) {
return addHandler(handler, TextChangeEvent.TYPE);
}
}
A TextBox should fire a ChangeEvent
(or ValueChangeEvent
- don't know at the moment). So you could easily do something like this inside your constructor:
this.addChangeHandler(new ChangeHandler ...
If that's not enough, you could also listen to the Focus
event.
Thanks for your help. I figured it out by myself today and it is quiet simple. I didn't notice, that there is already the DropHandler
.
You can do this for example in the constructor:
addDropHandler(new DropHandler() {
@Override
public void onDrop(DropEvent event) {
if(!event.getDataTransfer().getData("text/plain").isEmpty()) {
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
fireEvent(new TextChangeEvent(ExtendedTextBox.this.getText()));
}
});
}
}
});
To prevent the default behaviour of the TextField
just add event.preventDefault();
. Now you can get the input by calling event.getDataTransfer().getData("text/plain")
and do your input validation etc.
And as side effect the onBrowserEvent(Event event)
method is called now by dropping something into the text field. But the type int is -1
, so it's not really helpful at this place.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.