1

I have a my own widget which simulates a multi select list box. It will have a list of check boxes.

  public class MultiListBox extends Composite implements IsWidget
  {
    private static MultiListBoxUiBinder uiBinder = GWT
        .create(MultiListBoxUiBinder.class);

    interface MultiListBoxUiBinder extends
        UiBinder<Widget, MultiListBox> {
    }

    public MultiListBox() {
       initWidget(uiBinder.createAndBindUi(this));
    }
    @UiField ScrollPanel scrollPanel;
    @UiField FlowPanel flowPanel;

    private final List<CheckBox> checkboxes = new ArrayList<CheckBox>();
    private final List<String> selectedValues = new  ArrayList<String>();

@Override
public void addItem(final String value, final String text){

    final CheckBox checkbox = new CheckBox(text);
    checkbox.setFormValue(value);

    checkbox.addClickHandler(new ClickHandler()
    {
        public void onClick(final ClickEvent event)
        {
            final CheckBox chkbox = (CheckBox)event.getSource();
            if(chkbox.getValue())
            {
                selectedValues.add(value);
            }
            else
            {
                selectedValues.remove(value);
            }
        }
    });

    flowPanel.add(checkbox);
    checkboxes.add(checkbox);
}

@Override
public List<String> getSelectedValues(){
    return selectedValues;
}

@Override
public void clear() {
    checkboxes.clear();
    selectedValues.clear();
    flowPanel.clear();
}

@Override
public int getItemCount() {
    return checkboxes.size();
}

  }

I am consuming this in my views like

  <my:MultiListBox ui:field="myList"></my:MultiListBox>

Now, I wanted to create a onClick handler for this "myList" field in my view like

    @UiHandler("myList")
    void onMyListCliced(ClickEvent e) {
         //TODO: handle it
    }

Can anyone please guide how to achieve it.

Regards, Sree

Sree
  • 921
  • 2
  • 12
  • 31

1 Answers1

3

Your MultiListBox has to implement interface HasClickHandlers (in case of click event). If you want to allow usage of other events via UiHandler there is a bunch of other interfaces in a form of Has*Handlers, which you will have to implement on your custom widget.

jusio
  • 9,850
  • 1
  • 42
  • 57
  • Thank you So much for the reply... I added the below method to MY MultiListBox.java class`code' @Override public HandlerRegistration addClickHandler(ClickHandler handler) { return this.addHandler(handler, ClickEvent.getType()); } and handled a Clickevent @UiHandler("specialties") void onSpecialtyChange(ClickEvent e) { } But I dont get into this method, when I clik the checkboxes in the widget. The check Boxes will be added dynamically. – Sree Dec 03 '12 at 00:10
  • I don't think that I understand what is your problem now. Where did you add `onSpecialtyChange(ClickEvent e)` ? – jusio Dec 03 '12 at 12:06
  • oh... please ignore my previous reply. It doesnt get into onMyListCliced(ClickEvent e) Actually, the checkbox widgets are getting added dynamically. – Sree Dec 03 '12 at 17:48
  • I solved the issue. I modified the addItem() method.. add the line within the checkbox click handler fireEvent(event); Thank you so much for your effor jusio – Sree Dec 03 '12 at 19:07