I am working on implementing a ListBox that I want to be able to alert the user when they make a selection in the ListBox. Is there a way to respond to the user clicking an item in the listbox, and respond to the selection before a "changeEvent" occurs so I can prevent the changeEvent from getting fired. I have tried using
Event.addNativePreviewHandler(new NativePreviewHandler() {
@Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
System.out.println("EVENT: " + event.getTypeInt());
}
});
but this never responds to clicking on a specific element in the ListBox, it only responds to clicking on the ListBox initially, when you focus the ListBox. I want to be able to do something like:
Event.addNativePreviewHandler(new NativePreviewHandler() {
@Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if(event is changeEvent && event source is listBox) {
if(do some check here)
event.stopPropagation();
}
}
});
Any suggestions would be greatly appreciated, thanks!
You can simply kill the event if you don't want to treat it:
@Override
onChange(ChangeEvent event) {
// ask user if he wants to continue
// if yes
doSomething();
// if no
event.kill();
}
Thanks everyone for your input. What I ended up doing was saving the state of the selection when the list box gets focused. And saving this index and switching it when the changeEvent occurs only changing it to the next selection if permitted.
@UiHandler("listBox")
public void onListBoxFocus(FocusEvent event) {
this.saveListBoxSelection = listBox.getSelectedIndex();
}
@UiHandler("listBox")
public void onChange(ChangeEvent event) {
int newSelection = listBoxCompliant.getSelectedIndex();
listBox.setSelectedIndex(this.saveListBoxSelection);
if(proceed with change)
listBox.setSelectedIndex(newSelection);
else
//cancel event
This is done using a uibinder, but can also be done by simply adding this events directly to the listBox. Thanks again for the comments!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With