Domanda

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

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top