最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - GWT - ListBox, how to preview changeEvent - Stack Overflow

programmeradmin1浏览0评论

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!

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!

Share Improve this question asked Sep 25, 2012 at 20:24 Lakota LeflerLakota Lefler 1513 silver badges7 bronze badges 1
  • @AndreiVolgin thanks for your response. I want to be able to allow them to click on the options, and then prompt them with a popup, to agree to continue, and then to continue to execute the changeEvent or cancel the changeEvent based on their response to the popup. – Lakota Lefler Commented Sep 25, 2012 at 21:11
Add a ment  | 

3 Answers 3

Reset to default 3

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 ments!

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

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

评论列表(0)

  1. 暂无评论