Search code examples
drag-and-dropzk

ZK MVC Drag and Drop between two Listbox


I have a problem in ZK MVC controller.

I'd like to manage drag and drop between 2 Listbox (right and left).

This code loads item in right:

for(int i=0;i<lstEtic.size();i++) {
        Listitem li = new Listitem();
        System.out.println(lstEtic.get(i));
        addListcell(li,lstEtic.get(i));
        right.appendChild(li);

    }

The Left listbox is empty.

Question: Will someone please give me a code example to manage drag and drop events in Java controller?


Solution

  • Here is an example based on the documentation I mentioned. It is very easy to move all the necessary attribute setting and onDrop listeners to Java:

    <hlayout width="400px" height="400px" apply="path.to.MyComposer">
        <listbox id="left" hflex="1" vflex="1" />
        <listbox id="right" hflex="1" vflex="1" />
    </hlayout>
    
    public class MyComposer
        extends SelectorComposer<Component>
    {
        @Wire
        private Listbox left;
        @Wire
        private Listbox right;
    
        @Override
        public void doAfterCompose(Component comp)
            throws Exception
        {
            super.doAfterCompose(comp);
    
            for (int i = 0; i < 10; i++)
            {
                Listitem li = new Listitem();
                li.appendChild(new Listcell("Item " + i));
                li.setDraggable("true");
                right.appendChild(li);
            }
    
            left.setDroppable("true");
            left.addEventListener(Events.ON_DROP,
                                  (DropEvent event) -> left.appendChild(event.getDragged()));
        }
    }