Drag and Drop of an TreeGrid

Hey Vaadin-Users!
I’m not able to get my TreeGrid Drag and Drop like an Grid. I’m using the newest version of vaadin 8.1.0.rc1. I always get the same error and I don’t know what i should use instead of ListDataProvider. When I’m using TreeDataProvider I get errors with .getItems() function :

[code]
15:42:29,998 SEVERE [com.vaadin.server.DefaultErrorHandler]
(default task-94) : java.lang.ClassCastException: com.vaadin.data.provider.TreeDataProvider cannot be cast to com.vaadin.data.provider.ListDataProvider
at org.test.MyUI.lambda$9(MyUI.java:123)
at java.util.Optional.ifPresent(Optional.java:159)
at org.test.MyUI.lambda$8(MyUI.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:211)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:174)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1029)
at com.vaadin.ui.components.grid.TreeGridDropTarget.lambda$registerDropTargetRpc$4d5e56ef$1(TreeGridDropTarget.java:86)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:155)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:116)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:445)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:410)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:274)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:90)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1577)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:381)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
at io.undertow.websockets.jsr.JsrWebSocketFilter.doFilter(JsrWebSocketFilter.java:130)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

15:42:30,044 SEVERE [com.vaadin.server.DefaultErrorHandler]
(default task-95) : java.lang.ClassCastException: com.vaadin.data.provider.TreeDataProvider cannot be cast to com.vaadin.data.provider.ListDataProvider
at org.test.MyUI.lambda$7(MyUI.java:107)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:211)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:174)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1029)
at com.vaadin.ui.components.grid.GridDragSource.access$200(GridDragSource.java:48)
at com.vaadin.ui.components.grid.GridDragSource$1.dragEnd(GridDragSource.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:155)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:116)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:445)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:410)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:274)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:90)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1577)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:381)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
at io.undertow.websockets.jsr.JsrWebSocketFilter.doFilter(JsrWebSocketFilter.java:130)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
[/code]this is my Java-File:

package org.test;
import java.io.File;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import javax.servlet.annotation.WebServlet;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.data.TreeData;
import com.vaadin.data.provider.ListDataProvider;
import com.vaadin.data.provider.TreeDataProvider;
import com.vaadin.icons.VaadinIcons;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.shared.ui.dnd.DropEffect;
import com.vaadin.shared.ui.dnd.EffectAllowed;
import com.vaadin.shared.ui.grid.DropLocation;
import com.vaadin.shared.ui.grid.DropMode;
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Tree;
import com.vaadin.ui.TreeGrid;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.components.grid.GridDragSource;
import com.vaadin.ui.components.grid.GridDropTarget;
import com.vaadin.ui.components.grid.TreeGridDragSource;
import com.vaadin.ui.components.grid.TreeGridDropTarget;
import com.vaadin.ui.dnd.DragSourceExtension;
import com.vaadin.ui.dnd.DropTargetExtension;
import com.vaadin.ui.renderers.DateRenderer;
import com.vaadin.ui.renderers.HtmlRenderer;
import com.vaadin.ui.themes.ValoTheme;
/**
 * This UI is the application entry point. A UI may either represent a browser window 
 * (or tab) or some part of a html page where a Vaadin application is embedded.
 * <p>
 * The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be 
 * overridden to add component to the user interface and initialize non-component functionality.
 */
@Theme("mytheme")
public class MyUI extends UI {
private Set<Menu> draggedItems;
@Override
    protected void init(VaadinRequest vaadinRequest) {
        final HorizontalLayout layout = new HorizontalLayout();
        
        
        TreeGrid<Menu> treeGrid = new TreeGrid<>();
        treeGrid.setItems(generateMenusForYears(2010, 2016), Menu::getSubMenus);
        TextField nameEditor = new TextField();
 
        treeGrid.addColumn(Menu::getName).setCaption("Project Name").setId("name-column").setEditorComponent(nameEditor, Menu::setName);
        treeGrid.getEditor().setEnabled(true);
        
//        treeGrid.addContextClickListener(event -> Notification.show((TreeContextClickEvent<Person>)event).getItem() + " Clicked");
 
        treeGrid.addCollapseListener(event -> {
            Notification.show(
                    "Menu '" + event.getCollapsedItem().getName() + "' collapsed.",
                    Type.TRAY_NOTIFICATION);
        });
        treeGrid.addExpandListener(event -> {
            Notification.show(
                    "Menu '" + event.getExpandedItem().getName()+ "' expanded.",
                    Type.TRAY_NOTIFICATION);
        });
        
        
        TreeGridDragSource<Menu> dragSource = new TreeGridDragSource<>(treeGrid);
    // set allowed effects
    dragSource.setEffectAllowed(EffectAllowed.MOVE);
    // add a drag data generator
    dragSource.setDragDataGenerator("text", menu -> {
        return menu.getName();
    });
    
dragSource.addGridDragStartListener(event ->
    // Keep reference to the dragged items
    draggedItems = event.getDraggedItems()
);
 
    // Add drag end listener  
    dragSource.addGridDragEndListener(event -> {
        // If drop was successful, remove dragged items from source Grid
        if (event.getDropEffect() == DropEffect.MOVE) {
            ((ListDataProvider<Menu>) treeGrid.getDataProvider()).getItems().removeAll(draggedItems);
            treeGrid.getDataProvider().refreshAll();
            // Remove reference to dragged items
            draggedItems = null;
        }
    });
    
    TreeGridDropTarget<Menu> dropTarget = new TreeGridDropTarget<>(treeGrid, DropMode.BETWEEN);
    dropTarget.setDropEffect(DropEffect.MOVE);
    
    dropTarget.addTreeGridDropListener(event -> {
       // Accepting dragged items from another Grid in the same UI
       event.getDragSourceExtension().ifPresent(source -> {
           if (source instanceof TreeGridDragSource) {
               // Get the target Grid's items
               ListDataProvider<Menu> dataProvider = (ListDataProvider<Menu>)
                       event.getComponent().getDataProvider();
               List<Menu> items = (List<Menu>) dataProvider.getItems();
               // Calculate the target row's index
               int index = items.size();
               if (event.getDropTargetRow().isPresent()) {
                   index = items.indexOf(event.getDropTargetRow().get()) + (
                       event.getDropLocation() == DropLocation.BELOW ? 1 : 0);
               }
               // Add dragged items to the target Grid
               items.addAll(index, draggedItems);
               dataProvider.refreshAll();
               // Show the dropped data
               Notification.show("Dropped row data: " + event.getDataTransferText());
           }
       });
    });
        
        
        
        layout.addComponent(treeGrid);
        
        
        treeGrid.setWidth("15%");
        /*
        tree.setDataProvider(new TreeDataProvider<>(data));
        tree.expand("Desktops", "Laptops");
        tree.setItemIconGenerator(item -> {
            if (item.equals("Desktops")) {
                return VaadinIcons.DESKTOP;
            } else if (item.equals("Laptops")) {
                return VaadinIcons.LAPTOP;
            }
            return null;
        }); 
        */
        
        Label draggableLabel = new Label("You can grab and drag me");
        DragSourceExtension<Label> dragSource1 = new DragSourceExtension<>(draggableLabel);
        // set the allowed effect
        dragSource1.setEffectAllowed(EffectAllowed.MOVE);
        // set the text to transfer
        dragSource1.setDataTransferText("hello receiver");
        // set other data to transfer (in this case HTML)
        dragSource1.setDataTransferData("text/html", "<label>hello receiver</label>");
        
        dragSource1.addDragStartListener(event -> { Notification.show("Drag event started"); });
        dragSource1.addDragEndListener(event -> {
       if (event.isCanceled()) {
           Notification.show("Drag event was canceled");
       } else {
           Notification.show("Drag event finished");
       }
   });
        
        
        VerticalLayout dropTargetLayout = new VerticalLayout();
        dropTargetLayout.setCaption("Drop things inside me");
        dropTargetLayout.addStyleName(ValoTheme.LAYOUT_CARD);
        // make the layout accept drops
        DropTargetExtension<VerticalLayout> dropTarget1 = new DropTargetExtension<>(dropTargetLayout);
        // the drop effect must match the allowed effect in the drag source for a successful drop
        dropTarget1.setDropEffect(DropEffect.MOVE);
        // catch the drops
        dropTarget1.addDropListener(event -> {
            // if the drag source is in the same UI as the target
            Optional<AbstractComponent> dragSource2 = event.getDragSourceComponent();
            if (dragSource2.isPresent() && dragSource2.get() instanceof Label) {
                // move the label to the layout
                dropTargetLayout.addComponent(dragSource2.get());
                // get possible transfer data
                String message = event.getDataTransferText();
                if (message != null) {
                    Notification.show("DropEvent with data transfer html: " + message);
                } else {
                    // get transfer text
                    message = event.getDataTransferText();
                    Notification.show("DropEvent with data transfer text: " + message);
                }
                // handle possible server side drag data, if the drag source was in the same UI
//                event.getDragData().ifPresent(data -> handleMyDragData((MyObject) data));
            }
        });
        
        layout.addComponent(treeGrid);
        VerticalLayout components = new VerticalLayout();
        components.addComponent(draggableLabel);
        components.addComponent(dropTargetLayout);
        layout.addComponent(components);
        setContent(layout);
    }
    
    private List<Menu> generateMenusForYears(int startYear, int endYear) {
        List<Menu> menus = new ArrayList<>();
 
        for (int year = startYear; year <= endYear; year++) {
            Menu yearMenu = new Menu("Year " + year);
 
            for (int i = 1; i < 2 + ThreadLocalRandom.current().nextInt(5); i++) {
                Menu customerMenu = new Menu("Customer Project " + i);
                customerMenu.setSubMenus(Arrays.asList(
                        new LeafMenu("Implementation", ThreadLocalRandom.current().nextInt(100), year),
                        new LeafMenu("Planning", ThreadLocalRandom.current().nextInt(10), year),
                        new LeafMenu("Prototyping", ThreadLocalRandom.current().nextInt(20), year)));
                yearMenu.addSubMenu(customerMenu);
            }
            menus.add(yearMenu);
        }
        return menus;
    }
    
    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }
}

I would be really thankful if someone could help.

Best regards,
Dominik

I can not really analyse your code until I have your Menu and Leafmenu implementations. Please provide them as well or share your example at github.

Here is the Menu.Java:[code]
package org.test;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
public class Menu {

   private List<Menu> subMenus = new ArrayList<>();
   private String name;

   public Menu(String name) {
       this.name = name;
   }
  
   
   public String getName() {
       return name;
   }
   
   public void setName(String name) {
      this.name = name;
   }

   public List<Menu> getSubMenus() {
       return subMenus;
   }

   public void setSubMenus(List<Menu> subMenus) {
       this.subMenus = subMenus;
   }

   public void addSubMenu(Menu subMenu) {
       subMenus.add(subMenu);
   }

}
[/code]and here the LeafMenu.Java[code]
package org.test;
import java.sql.Date;
public class LeafMenu extends Menu {

public LeafMenu(String name) {
super(name);
}
public boolean isLeaf() {
return true;
}
}
[/code]Thank you in advance! :slight_smile:

You are setting data items to the TreeGrid via
TreeGrid.setItems
. Actually this is a shortcut method which makes a
TreeData
object and a
TreeDataProvider
on top of it. Data providers in general are not supposed to handle any of data manipulations but only provide data to UI components. If you need to dinamically manipulate the data, I suggest you to make a field of type
TreeData
, fill that with your items, and manipulate with the items using
TeeData
API.

Like this:

private TreeData<Menu> treeData = new TreeData<>();
...

treeData.addItems(generateMenusForYears(2010, 2016), Menu::getSubMenus);
TreeGrid<Menu> treeGrid = new TreeGrid<>(new TreeDataProvider<>(treeData));

...
if (event.getDropEffect() == DropEffect.MOVE) {
            draggedItems.forEach(treeData::removeItem);
             treeGrid.getDataProvider().refreshAll();
             draggedItems = null;
}

and how to drop the Data in the TreeGrid? :-/
This throws an error:

TreeGridDropTarget<Menu> dropTarget = new TreeGridDropTarget<>(treeGrid, DropMode.BETWEEN);
   dropTarget.setDropEffect(DropEffect.MOVE);
   
   dropTarget.addTreeGridDropListener(event -> {
       // Accepting dragged items from another Grid in the same UI
       event.getDragSourceExtension().ifPresent(source -> {
           if (source instanceof GridDragSource) {
               // Get the target Grid's items
               TreeDataProvider<Menu> dataProvider = (TreeDataProvider<Menu>)
                       event.getComponent().getDataProvider();
               List<Menu> items = (List<Menu>) dataProvider.getItems();
               // Calculate the target row's index
               int index = items.size();
               if (event.getDropTargetRow().isPresent()) {
                   index = items.indexOf(event.getDropTargetRow().get()) + (
                       event.getDropLocation() == DropLocation.BELOW ? 1 : 0);
               }
               // Add dragged items to the target Grid
               items.addAll(index, draggedItems);
               dataProvider.refreshAll();
               // Show the dropped data
               Notification.show("Dropped row data: " + event.getDataTransferText());
           }
       });
   });

I also find it hard to accomplish reordering the TreeGrid.
Opened a ticket: https://github.com/vaadin/framework/issues/9750