Getting selected ListBox values on button Click | ZK

Umesh Awasthi picture Umesh Awasthi · Jan 9, 2014 · Viewed 7.2k times · Source

I am very new to ZK framework and trying to customize few things and have struck at one point which I am not sure how to achieve that.

I have a predefined section where I need to show 2 drop down and a button and need to persist those drop down values on button click event. This is how It has been define in Spring file

<bean id="mybean" parent="parentBean" class="WidgetRenderer">
   <property name="detailRenderer">
            <bean class="DetailsListRenderer" parent="abstractWidgetDetailRenderer"/>
   </property>
</bean>

Here mybean is being used to show main section and I am adding my drop down using this bean while button are being added to detailRenderer.

Save button is bind to onClick event, but I am not sure how I can fetch values from my custom drop down? I am aware about binding those Dropdown with onClick event but they have to be in same class.

Can any one suggest me how I can fetch values of those drop down.I am creating down down with following code

Listbox listbox = new Listbox();
listbox.appendItem("item1", "item1");
listbox.appendItem("item2", "item2");

This is my button code in another class

protected void createUpdateStatusButton(Widget widget,Div container)
 {
   Button button = new Button(LabelUtils.getLabel(widget, buttonLabelName, new Object[0]));
   button.setParent(container);
    button.addEventListener("onClick", new EventListener()
    {
      public void onEvent(Event event)throws Exception
       {
             MyClass.this.handleSaveStatusEvent(widget, event);
          }
    });
  }

Answer

Sean Connolly picture Sean Connolly · Jan 9, 2014

You may want to listen to the onSelect (I prefer to use Events.ON_SELECT rather than writing the strings) which is more specific to when the Listbox selection changes.

Either way, the key is to get the information you want from the Event passed to your EventListener, rather than going back to your Listbox itself. The basic Event usually carries useful information on getTarget and getData but using more specific events (SelectEvent in this case) will give you access to more relevant info.

button.addEventListener(Events.ON_SELECT, new EventListener<SelectEvent<Listitem, MyDataObject>() {
    public void onEvent(SelectEvent<Listitem, MyDataObject> event) {
        // Now you can access the details of the selection event..
        List<Listitem> selectedItems = event.getSelectedItems();
        List<MyDataObject> selectedObjects = event.getSelectedObjects();
    }
});

You can find out what events are available for different ZK widgets in their Component Reference documentation.