GWT - ListBox how to view changeEvent

I am working on a ListBox implementation that I want to alert the user when they make a selection in the ListBox. Is there a way to respond to the user by clicking an item in the list and answer the selection before “changeEvent” occurs, so that I can prevent changeEvent from changing. I tried using

Event.addNativePreviewHandler(new NativePreviewHandler() {
   @Override
   public void onPreviewNativeEvent(NativePreviewEvent event) {
      System.out.println("EVENT: " + event.getTypeInt());
   }
});

but it never responds to clicking on a specific item in a ListBox, it only responds to clicking on a ListBox 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!

+5
source share
3 answers

. , , - , . , changeEvent , , .

@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

uibinder, , listBox. !

+4

, :

@Override
onChange(ChangeEvent event) {
    // ask user if he wants to continue
    // if yes
       doSomething();
    // if no
       event.kill();
}
+3

There is no harm in allowing ChangeEvent to continue. You can add your logic to the ChangeEvent handler. If the user decides not to continue, you simply set the selection to the previous one or remove the selection.

@Override
onChange(ChangeEvent event) {
    // ask user if he wants to continue
    // if yes
       doSomething();
    // if no
       myList.setSelectedIndex(-1);
} 
+1
source

All Articles